@Import({SimpleStart.MemberClass.class})
public class SimpleStart {
public SimpleStart(ExampleBean exampleBean) {
}
static class MemberClass implements ImportBeanDefinitionRegistrar {
private Environment environment;
public MemberClass(Environment environment) {
this.environment = environment;
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
if(environment.getProperty("new-config.enable", Boolean.class,false)){
registry.removeBeanDefinition("oldBeanConfig");
registry.registerBeanDefinition(NewBeanConfig.class.getName(), BeanDefinitionBuilder.genericBeanDefinition(NewBeanConfig.class).getBeanDefinition());
}
}
}
}
public class NewBeanConfig {
@Bean
ExampleBean exampleBean(){
return new ExampleBean();
}
}
public class OldBeanConfig{
}
public class ExampleBean {
}
@Test
public void research(){
System.setProperty("new-config.enable","true");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SimpleStart.class,OldBeanConfig.class);
ExampleBean bean = context.getBean(ExampleBean.class);
}
The above code will cause the ExampleBean to not be obtained,When I saw ConfigurationClassPostProcessor source code,I found processConfigBeanDefinitions method have if (registry.getBeanDefinitionCount() > candidateNames.length) {} lead to no more process NewBeanConfig ConfigClass.I don't see why did it.
Comment From: snicoll
SimpleSart
is not annotated with @Configuration
. This turns what is called "the lite mode" where only basic methods are checked. NewBeanConfig
should also be flagged with @Configuration
.
If you have further questions, please ask on StackOverflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements.