-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
Background: When trying to explore the source of dependency injection in the 5.2.2.RELEASE of the Spring Framework, I found a bug ((just a personal opinion)). Here is an example:
@Configuration
public class AtAutowiredAnnotationInjectionDemoV2 {
@Autowired
private Collection<UserEntity> userEntities;
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AtAutowiredAnnotationInjectionDemoV2.class, BeanConfig.class);
context.refresh();
AtAutowiredAnnotationInjectionDemoV2 demo = context.getBean(AtAutowiredAnnotationInjectionDemoV2.class);
System.out.println(demo.userEntities);
context.close();
}
@Bean("user1")
public UserEntity user1() {
UserEntity userEntity = new UserEntity();
userEntity.setId(1L);
userEntity.setUsername("Markus Zhang");
return userEntity;
}
@Bean("user2")
@Primary
public UserEntity user2() {
UserEntity userEntity = new UserEntity();
userEntity.setId(2L);
userEntity.setUsername("Luna");
return userEntity;
}
}
@Configuration
public class BeanConfig {
@Bean
public UserEntity userByConfig() {
UserEntity userEntity = new UserEntity();
userEntity.setId(3L);
userEntity.setUsername("markus zhang from bean config");
return userEntity;
}
}
public class UserEntity {
private Long id;
private String username;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "UserEntity{" +
"id=" + id +
", username='" + username + '\'' +
'}';
}
}
The code is Simple introduced as followed: UserEntity is a POJO, BeanConfig and AtAutowiredAnnotationInjectionDemoV2 are two configuration class, I start in the main function of AtAutowiredAnnotationInjectionDemoV2 AnnotationConfigApplicationContext and by dependency lookup Get the Spring Bean AtAutowiredAnnotationInjectionDemoV2, and print the users field, expectations should be three UserEntity Spring Bean,They are user1, user2, and userByConfig. But only the userByConfig Bean from the BeanConfig configuration class is actually injected. The result is as follows:
Similarly, I modified the main
function to remove the BeanConfig configuration class from the application context, such as in the following code:
@Configuration
public class AtAutowiredAnnotationInjectionDemoV2 {
@Autowired
private Collection<UserEntity> userEntities;
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// modified
context.register(AtAutowiredAnnotationInjectionDemoV2.class);
context.refresh();
AtAutowiredAnnotationInjectionDemoV2 demo = context.getBean(AtAutowiredAnnotationInjectionDemoV2.class);
System.out.println(demo.userEntities);
context.close();
}
@Bean("user1")
public UserEntity user1() {
UserEntity userEntity = new UserEntity();
userEntity.setId(1L);
userEntity.setUsername("Markus Zhang");
return userEntity;
}
@Bean("user2")
@Primary
public UserEntity user2() {
UserEntity userEntity = new UserEntity();
userEntity.setId(2L);
userEntity.setUsername("Luna");
return userEntity;
}
}
The UserEntity annotated by the @Bean
in the current class is injected, and the result is as follows:
Tracing the source,I found that it took place at org.springframework.beans.factory.support.DefaultListableBeanFactory#findAutowireCandidates stage, Instead of injecting Spring beans from the current configuration class, it is returned early, resulting in only UserEntity beans that are not registered by the current configuration class being injected
In summary, I think this case is a bug, the above is only a personal opinion, may not be correct. I am looking forward to your reply.
Have a nice day!