@Value 注入屬性值(補充)

在@Value 注入屬性值(下)中發現
@Value註解註釋的屬性是在InstantiationAwareBeanPostProcessor的postProcessPropertyValues方法注入到bean中的。
但是當時並沒有發現是在哪一個實現了postProcessPropertyValues接口的類中對@value註解進行了注入。
今天通過學習@Autowire註解的注入時機,有了新的發現

 1public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessorPriorityOrderedBeanFactoryAware {
2            private final Set<Class<? extends Annotation>> autowiredAnnotationTypes = new LinkedHashSet();
3
4      public AutowiredAnnotationBeanPostProcessor() {
5        this.autowiredAnnotationTypes.add(Autowired.class);
6        this.autowiredAnnotationTypes.add(Value.class);
7
8        try {
9            this.autowiredAnnotationTypes.add(ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));
10            this.logger.info("JSR-330 \'javax.inject.Inject\' annotation found and supported for autowiring");
11        } catch (ClassNotFoundException var2) {
12            ;
13        }
14
15    }
16}

AutowiredAnnotationBeanPostProcessor是InstantiationAwareBeanPostProcessor 的一個實現類。
autowiredAnnotationTypes 是AutowiredAnnotationBeanPostProcessor類所能處理的註解。
而正好在AutowiredAnnotationBeanPostProcessor類的構造函數中添加了兩個註解類
Autowired.class
Value.class
所以可以看出@Autowired和@Value註解都是通過AutowiredAnnotationBeanPostProcessor實現注入的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章