@Configuration中的来声明BeanPostProcessor和BeanFactoryPostProcessor

最近在遇到比较一个问题,通常我们在spring中使用@Configuration来注解一个类时,可以同时用Autowire 和@Value 给该配置类自动注入属性值,如下:


@Configuration
public class ConfigA {
   @Autowired
   private Environment environment;
   @Value("${name}")
   private string name;
   @Bean
   public A a() {
       A a = new A();
       a.name = name;
       return a;
   }
}

我们正常情况下都是能自动注入environment和name的,但是当在configration 中如果声明了一个BeanPostProcessor时,如下:

   @Bean
    public MyBeanPostProcessor myBeanPostProcessor(A a) {
        log.info("env=:{}", environment);
        MyBeanPostProcessor myBeanPostProcessor = new MyBeanPostProcessor();
        myBeanPostProcessor.setA(a);
        return myBeanPostProcessor;
    }

你会发现打出来的log env 为null,同时A的name 也为null,这个配置类的 @Autowired和@Value都没有生效。
翻阅spring 的官网,其中有一段小tip,如下

    
Make sure that the dependencies you inject that way are of the simplest kind only. @Configuration classes are processed quite early during the initialization of the context, and forcing a dependency to be injected this way may lead to unexpected early initialization. Whenever possible, resort to parameter-based injection, as in the preceding example.

Also, be particularly careful with BeanPostProcessor and BeanFactoryPostProcessor definitions through @Bean. Those should usually be declared as static @Bean methods, not triggering the instantiation of their containing configuration class. Otherwise, @Autowired and @Value may not work on the configuration class itself, since it is possible to create it as a bean instance earlier than AutowiredAnnotationBeanPostProcessor.

意思就是BeanPostProcessor 和BeanFactoryPostProcessor 会在configuration 本身这个bean初始化之前,可以在spring onrefresh 方法中找到这段代码

image.png

踩坑记录一下。

评论

暂无评论