spring中管理的Bean的创建流程

来源:互联网 发布:个人卡通形象设计软件 编辑:程序博客网 时间:2024/05/11 00:13
 

Spring的生命周期.

 

1.       容器启动,实例化所有实现了BeanFactoyPostProcessor接口的类。他会在任何普通Bean实例化之前加载.

2.       实例化剩下的Bean,对这些Bean进行依赖注入。

3.       如果Bean有实现BeanNameAware的接口那么对这些Bean进行调用

4.       如果Bean有实现BeanFactoryAware接口的那么对这些Bean进行调用

5.       如果Bean有实现ApplicationContextAware接口的那么对这些Bean进行调用

6.       如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessBeforeInitialization方法

7.       如果Bean有实现InitializingBean接口那么对这些Bean进行调用

8.       如果Bean配置有init属性,那么调用它属性中设置的方法

9.       如果配置有实现BeanPostProcessor的Bean,那么调用它的postProcessAfterInitialization方法

10.   Bean正常是使用

11.   调用DisposableBean接口的destory方法

12.   调用Bean定义的destory方法

 

如果从大体上区分值分只为四个阶段

1.       BeanFactoyPostProcessor实例化

2.       Bean实例化,然后通过某些BeanFactoyPostProcessor来进行依赖注入

3.       BeanPostProcessor的调用.Spring内置的BeanPostProcessor负责调用Bean实现的接口: BeanNameAware, BeanFactoryAware, ApplicationContextAware等等,等这些内置的BeanPostProcessor调用完后才会调用自己配置的BeanPostProcessor

4.       Bean销毁阶段

 

 

以上是自己总结的,没研究过源码,恐有误…作参考用

以下附上验证的代码:

package mislay;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor {@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {System.out.println("--------> begin BeanFactoryPostProcessorTest");String[] names = beanFactory.getBeanDefinitionNames();for (String name : names) {System.out.println("definition bean name:" + name);}System.out.println("<--------- end BeanFactoryPostProcessorTest");}}


 

package mislay;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;public class BeanPostProcessorTest implements BeanPostProcessor {@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName)throws BeansException {System.out.println("call BeanPostProcessor interface postProcessAfterInitialization method; :" + beanName);return bean;}@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName)throws BeansException {System.out.println("call BeanPostProcessor interface postProcessBeforeInitialization method ::" + beanName);if(bean instanceof BeanTest) {System.out.println("bean instanceof BeanTest");}return bean;}}


 

package mislay;import org.springframework.beans.BeansException;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.BeanFactoryAware;import org.springframework.beans.factory.BeanNameAware;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class BeanTest implements InitializingBean, DisposableBean,BeanNameAware,
BeanFactoryAware,ApplicationContextAware {@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("call InitializingBean interface");}@Overridepublic void destroy() throws Exception {// TODO Auto-generated method stubSystem.out.println("call DisposableBean interface");}public void _init() {System.out.println("call bean init method");}public void _destory() {System.out.println("call bean destory method");}public void setSomething(Object something) {System.out.println("DI call setSomething method");}public BeanTest() {System.out.println("BeanTest create");}@Overridepublic void setBeanName(String name) {// TODO Auto-generated method stubSystem.out.println("call BeanNameAware interface name is:" + name);}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {// TODO Auto-generated method stubSystem.out.println("call BeanFactoryAware interface");}@Overridepublic void setApplicationContext(ApplicationContext applicationContext)throws BeansException {// TODO Auto-generated method stubSystem.out.println("call ApplicationContextAware interface");}}



 

 
package mislay;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");((AbstractApplicationContext) context).registerShutdownHook();}}

 
//下面是输出的内容
//这一段可以看出首先实例化化的BeanFactoryPostProcessor--------> begin BeanFactoryPostProcessorTestdefinition bean name:mislay.BeanFactoryPostProcessorTestdefinition bean name:mislay.BeanPostProcessorTestdefinition bean name:beanTest<--------- end BeanFactoryPostProcessorTest//这一段是Bean的创建以及依赖注入BeanTest createDI call setSomething method//这一段就是内置的BeanPostProcessor调用call BeanNameAware interface name is:beanTestcall BeanFactoryAware interfacecall ApplicationContextAware interface//这里开始调用自己BeanPostProcessorcall BeanPostProcessor interface postProcessBeforeInitialization method ::beanTestbean instanceof BeanTest//穿插着对InitializingBean接口和定义的init方法的调用call InitializingBean interfacecall bean init methodcall BeanPostProcessor interface postProcessAfterInitialization method; :beanTest//上面就结束了对配置的BeanPostProcessor的调用//最后销毁call DisposableBean interfacecall bean destory method 
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 四川省住房城乡建设厅首页 新乡市住房公积金管理中心 住房贷款利率计算器 邵阳市住房公积金管理中心 邵阳住房公积金 商业住房贷款利率 陕西省住房公积金管理中心 合肥住房公积金管理中心 长春市住房公积金 山东省住房城乡建设厅 2019各银行住房商贷利率一览表 住房公积金查询入口 陕西省住房和城乡建设厅网 沧州住房公积金个人查询入口 包头市住房公积金管理中心 佛山住房公积金中心 个人住房商业性贷款 住房公积金装修贷款能贷多少 邵阳住房公积金管理中心 工资4000住房公积金一般交多少 西安住房保障管理局网站 佛山住房公积金 住房城乡建设部 河南省住房和城乡建设厅网 北京住房公积金 广州住房公积金管理中心 西安住房公积金 成都住房公积金管理中心 宜春住房公积金 安徽省住房和城乡建设厅 四川城乡住房建设厅 陕西省住房公积金中心 深圳市住房和建设局 南宁市住房保障和房产管理局 住房公积金是什么 十堰住房公积金查询 广安住房公积金查询 枣庄住房公积金管理中心 南宁市住房公积金查询 陕西住房公积金查询网 南充市住房公积金管理中心