spring的所有module的项目都有test资源,对于我来说真是一种福音,为什么呢,直接跑junit test即可知道单元类的作用了,spring-beans的test很完善,每个都可以跑。
先来说说spring的bean的生命周期
有几个很重要的接口
BeanFactoryPostProcessor
这个接口只有一个方法
/**
* Modify the application context's internal bean factory after its standard
* initialization. All bean definitions will have been loaded, but no beans
* will have been instantiated yet. This allows for overriding or adding
* properties even to eager-initializing beans.
* @param beanFactory the bean factory used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*/
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
容器启动的时候:
注释很明确 This allows for overriding or adding properties even to eager-initializing beans 可以对初始化的bean添加或者重写属性。
可以跑一下BeanFactoryPostProcessorTests里面的6个测试方法
这个类里面的测试方法可以知道 DefaultListableBeanFactory 是会默认创建的beanfactory 子类构造方法未传入的时候 会选择父类创建DefaultListableBeanFactory的实例
DefaultListableBeanFactory的registerBeanDefinition就是在注册bean到beanfactory 前面的文章提到bean注册中心的在这里得到了应正。
足矣见得Beanfactory类在spring中起到的关键作用了
实例化后调用:
InstantiationAwareBeanPostProcessor
InstantiationAwareBeanPostProcessor 继承自 BeanPostProcessor,
类说明Subinterface of {@link BeanPostProcessor} that adds a before-instantiation callback,and a callback after instantiation but before explicit properties are set or autowiring occurs. 大致意思就是初始化的回调和初始化后的回调,在属性设置之前或者自动装配发生的时候,
/**
* Post-process the given property values before the factory applies them
* to the given bean. Allows for checking whether all dependencies have been
* satisfied, for example based on a "Required" annotation on bean property setters.
* <p>Also allows for replacing the property values to apply, typically through
* creating a new MutablePropertyValues instance based on the original PropertyValues,
* adding or removing specific values.
* @param pvs the property values that the factory is about to apply (never {@code null})
* @param pds the relevant property descriptors for the target bean (with ignored
* dependency types - which the factory handles specifically - already filtered out)
* @param bean the bean instance created, but whose properties have not yet been set
* @param beanName the name of the bean
* @return the actual property values to apply to to the given bean
* (can be the passed-in PropertyValues instance), or {@code null}
* to skip property population
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.MutablePropertyValues
*/
PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
throws BeansException;
实例化后会执行这个方法。
初始化的时候 :
BeanNameAware接口
该接口有一个setBeanName的方法 比较容易理解 Set the name of the bean in the bean factory that created this bean.Invoked after population of normal bean properties but before an init callback such as {@link InitializingBean#afterPropertiesSet()} or a custom init-method.
BeanFactoryAware接口
该接口继承自Aware接口 有趣的是Aware接口没有任何规范在里面,BeanFactoryAware只有一个方法
void setBeanFactory(BeanFactory beanFactory) throws BeansException;
解释是这样的Callback that supplies the owning factory to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as{@link InitializingBean#afterPropertiesSet()} or a custom init-method.
ApplicationContextAware接口
该接口也继承Aware接口 是applicationContext的设置
方法void setApplicationContext(ApplicationContext applicationContext) throws BeansException;是比较重要的 因为applicationContext在整个spring中也扮演着非常重要的角色
Set the ApplicationContext that this object runs in. Normally this call will be used to initialize the object.Invoked after population of normal bean properties but before an init callback
BeanPostProcessor接口
Factory hook that allows for custom modification of new bean instances, e.g. checking for marker interfaces or wrapping them with proxies
工厂允许自定义修改一个刚刚新创建bean的实例的钩子
postProcessBeforeInitialization 初始化之前会被调用
InitializingBean接口
只有一个方法afterPropertiesSet
Invoked by a BeanFactory after it has set all bean properties supplied
在所有属性都被设置后调用
接下来就是bean自己的init-method属性执行
BeanPostProcessor的postProcessAfterInitialization方法 和postProcessBeforeInitialization 对应
最后是destory-method属性的执行
spring bean的生命周期基本就是这样,下篇具体分析test代码。