Spring's JavaConfig and CustomScopeConfigurer issue

前端 未结 1 1246
轮回少年
轮回少年 2020-12-11 02:32

I\'m seeing some odd behavior, I was hoping someone here can shine some light on the issue.

Let me start by describing my setup. First, a simple data object

相关标签:
1条回答
  • Copied from Spring @Bean javadoc:

    BeanFactoryPostProcessor-returning @Bean methods

    Special consideration must be taken for @Bean methods that return Spring BeanFactoryPostProcessor (BFPP) types. Because BFPP objects must be instantiated very early in the container lifecycle, they can interfere with processing of annotations such as @Autowired, @Value, and @PostConstruct within @Configuration classes. To avoid these lifecycle issues, mark BFPP-returning @Bean methods as static. For example:

    @Bean
     public static PropertyPlaceholderConfigurer ppc() {
         // instantiate, configure and return ppc ...
     }
    

    By marking this method as static, it can be invoked without causing instantiation of its declaring @Configuration class, thus avoiding the above-mentioned lifecycle conflicts. Note however that static @Bean methods will not be enhanced for scoping and AOP semantics as mentioned above. This works out in BFPP cases, as they are not typically referenced by other @Bean methods. As a reminder, a WARN-level log message will be issued for any non-static @Bean methods having a return type assignable to BeanFactoryPostProcessor.

    0 讨论(0)
提交回复
热议问题