BeanDefinitionRegistryPostProcessor - How to register a @Configuration class as BeanDefinition and get its @Beans registered as well

放肆的年华 提交于 2019-12-03 20:09:34

There reason why it didn't import the @Beans was that ConfigurationClassPostProcessor was executed before my postprocessor so the new beans weren't added. To solve it I implemented PriorityOrdered:

@Configuration
public class MyFactoryPostProcessor implements BeanDefinitionRegistryPostProcessor, PriorityOrdered{

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        BeanDefinition someConfig= new RootBeanDefinition("x.y.z.SomeConfig");
        registry.registerBeanDefinition("someConfig", enableOAuth2ClientBd);
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}

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:

@Configuration
public class BeanDefinitionFactoryTestConfig {

    @Bean
    public MyFactoryPostProcessor cc(){
        return new MyFactoryPostProcessor ();
    }   
}

-->> THIS WILL FAIL TO IMPORT THE BEANS<<--

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!