@Value 注入属性值(下)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/changyayun/article/details/81949672

@Value 注入属性值(下)

之前试验过@Value是在<init>对象实例化后,在注入属性(即调用类的set方法实现属性的初始化)前其作用的。
具体是如何起作用以及是什么时候其作用的。
下图是spring中一个bean的生命周期:


这里写图片描述

<init>对象实例化即时途中的“实例化”那一步。

注入属性即时图中的“设置属性值”。

那么@Value的作用时间可能在第三步或事第四步

public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
    Object postProcessBeforeInstantiation(Class<?> var1, String var2) throws BeansException;
​    boolean postProcessAfterInstantiation(Object var1, String var2) throws BeansException;
​    PropertyValues postProcessPropertyValues(PropertyValues var1, PropertyDescriptor[] var2, Object var3, String var4) throws BeansException;
}

InstantiationAwareBeanPostProcessor是BeanPostProcessor的子接口
postProcessPropertyValues方法可以扫描带有注解的字段和方法,并注入到Bean中
所以通过@Value注解注释的属性 就是在第四步中通过InstantiationAwareBeanPostProcessor的postProcessPropertyValues方法注入到bean中的。

猜你喜欢

转载自blog.csdn.net/changyayun/article/details/81949672
今日推荐