Let's suppose I have this @Configuration
class:
@Configuration
public class SomeConfig{
@Bean
public MyBean myBean(){
return new MyBean();
}
@Bean
public Another anotherBean(){
return new AnotherBean();
}
}
I have a class that implements BeanDefinitionRegistryPostProcessor
to add certain BeanDefinition
s. On it I also would like to import SomeConfig
so that its beans are added to the context:
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
BeanDefinition someConfig= new RootBeanDefinition("x.y.z.SomeConfig");
registry.registerBeanDefinition("someConfig", someConfig);
}
The problem is that SomeConfig
's beans (myBean
, anotherBean
) haven't been added to the context. There is a someConfig bean though:
@Autowired
MyBean myBean ---> FAILS
@Autowired
AnotherBean anotherBean ---> FAILS
@Autowired
SomeConfig someConfig ---> OK
There reason why it didn't import the
@Bean
s was thatConfigurationClassPostProcessor
was executed before my postprocessor so the new beans weren't added. To solve it I implementedPriorityOrdered
:It's also important that the postprocessor class is
@Configuration
and is imported directly in the config, not defined in another@Configuration
class with it defined as @Bean:-->> THIS WILL FAIL TO IMPORT THE BEANS<<--