Hi,I re-executed the program using version 5.3.27 of the spring framework, but the result is the same as 5.2.x and does not inject the expected results into the fields in the configuration class. You can also refer to the following code:
public class UserEntity {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "UserEntity{" +
"username='" + username + '\'' +
'}';
}
}
@Configuration
public class BeanConfigA {
@Autowired
private Collection<UserEntity> userEntities;
@Bean
public UserEntity userEntityA1() {
UserEntity userEntity = new UserEntity();
userEntity.setUsername("User A1");
return userEntity;
}
@Bean
public UserEntity userEntityA2() {
UserEntity userEntity = new UserEntity();
userEntity.setUsername("User A2");
return userEntity;
}
public Collection<UserEntity> getUserEntities() {
return userEntities;
}
}
@Configuration
public class BeanConfigB {
@Autowired
private Collection<UserEntity> userEntities;
@Bean
public UserEntity userEntityB1(){
UserEntity userEntity = new UserEntity();
userEntity.setUsername("User B1");
return userEntity;
}
@Bean
public UserEntity userEntityB2(){
UserEntity userEntity = new UserEntity();
userEntity.setUsername("User B2");
return userEntity;
}
public Collection<UserEntity> getUserEntities() {
return userEntities;
}
}
public class DependencyInjectionBugDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// register configuration class
context.register(BeanConfigA.class, BeanConfigB.class);
// refresh spring context
context.refresh();
Map<String, UserEntity> beansOfType = context.getBeansOfType(UserEntity.class);
System.out.println("ioc container include : ");
beansOfType.forEach((name, bean) -> System.out.println(name + ": " + bean.getUsername()));
// The Spring Bean into which the userEntities field in BeanConfigA is injected is expected to contain User A1、User A2、User B1、User B2
BeanConfigA beanConfigA = context.getBean(BeanConfigA.class);
System.out.println(beanConfigA.getUserEntities());
// The Spring Bean into which the userEntities field in BeanConfigB is injected is expected to contain User A1、User A2、User B1、User B2
BeanConfigB beanConfigB = context.getBean(BeanConfigB.class);
System.out.println(beanConfigB.getUserEntities());
// close spring context
context.close();
}
}
The result is as follows:
Comment From: markuszcl99
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!
Comment From: snicoll
Thanks but there's no need to create another issue. Brian already said we would reopen if you could reproduce with a supported version.
Duplicates #31903