浅析Spring IoC源码(十)Spring Bean的初始化顺序

上几节我们比较详细地说明了一下BeanFactoryPostProcessor和BeanPostProcessor这2个接口的作用和意义,并且也花了一个章节,讲了一下BeanFactory和FactoryBean的关系,最后我们也稍微说明了一下BeanFactoryAware和BeanNameAware这两个接口的作用,这一节,将开始讲一下,一个bean在被spring管理的时候,初始化时的顺序

我们定义一个简单的bean实现BeanFactoryPostProcessor,BeanPostProcessor,BeanNameAware,

BeanFactoryAware,InitializingBean这些接口,并且调用init-method,我们观察一下这些接口的具体实现的顺序,为我们下一节分析refresh做好最后的准备


照旧上代码

SpringSimpleMultiBean.Java

[java]  view plain  copy
  1. package org.study.spring.springinit;  
  2.   
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.BeanFactory;  
  5. import org.springframework.beans.factory.BeanFactoryAware;  
  6. import org.springframework.beans.factory.BeanNameAware;  
  7. import org.springframework.beans.factory.InitializingBean;  
  8. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;  
  9. import org.springframework.beans.factory.config.BeanPostProcessor;  
  10. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  11.   
  12. public class SpringSimpleMultiBean implements BeanFactoryAware,BeanNameAware,BeanFactoryPostProcessor,BeanPostProcessor,InitializingBean{  
  13.   
  14.     private Integer id;  
  15.       
  16.     private String name;  
  17.       
  18.     /** 
  19.      * 构造函数 
  20.      */  
  21.     public SpringSimpleMultiBean() {  
  22.           
  23.         System.out.println("构造函数 init");  
  24.     }  
  25.       
  26.       
  27.     public Integer getId() {  
  28.         return id;  
  29.     }  
  30.   
  31.     public void setId(Integer id) {  
  32.         this.id = id;  
  33.     }  
  34.   
  35.     public String getName() {  
  36.         return name;  
  37.     }  
  38.   
  39.     public void setName(String name) {  
  40.         this.name = name;  
  41.     }  
  42.   
  43.     /** 
  44.      * init-method 
  45.      */  
  46.     public void initMethod(){  
  47.         System.out.println("init method Begin");  
  48.     }  
  49.       
  50.       
  51.     public void say(){  
  52.         System.out.println("hello I am "+name);  
  53.     }  
  54.       
  55.   
  56.     /** 
  57.      * InitializingBean接口的具体实现 
  58.      */  
  59.     public void afterPropertiesSet() throws Exception {  
  60.         System.out.println("InitializingBean接口的具体实现 begin and id is "+ id +" and name is "+ name);  
  61.           
  62.     }  
  63.   
  64.     /** 
  65.      * BeanPostProcessor接口 before 初始化 
  66.      */  
  67.     public Object postProcessBeforeInitialization(Object bean, String beanName)  
  68.             throws BeansException {  
  69.         System.out.println("BeanPostProcessor接口 before 初始化  postProcessAfterInitialization begin");  
  70.         return bean;  
  71.     }  
  72.   
  73.     /** 
  74.      * BeanPostProcessor接口after 初始化 
  75.      */  
  76.     public Object postProcessAfterInitialization(Object bean, String beanName)  
  77.             throws BeansException {  
  78.         System.out.println("BeanPostProcessor接口after 初始化  postProcessAfterInitialization begin");  
  79.         return bean;  
  80.     }  
  81.   
  82.     /** 
  83.      * BeanFactoryPostProcessor 接口初始化 
  84.      */  
  85.     public void postProcessBeanFactory(  
  86.             ConfigurableListableBeanFactory beanFactory) throws BeansException {  
  87.         System.out.println("BeanFactoryPostProcessor 接口初始化 begin and this beanFactory is "+beanFactory);  
  88.           
  89.     }  
  90.   
  91.     /** 
  92.      * BeanNameAware初始化 
  93.      */  
  94.     public void setBeanName(String name) {  
  95.         System.out.println("BeanNameAware初始化 set BeanName begin and name is "+name);  
  96.     }  
  97.   
  98.     /** 
  99.      * BeanFactoryName初始化 
  100.      */  
  101.     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {  
  102.         System.out.println("BeanFactoryName初始化 begin and beanFactory is "+beanFactory);  
  103.     }  
  104.   
  105. }  
spring-init.xml

[java]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  8.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd  
  9.         http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd  
  10.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  
  11.    
  12.       
  13.             
  14.          <bean id="springMultiBean" class="org.study.spring.springinit.SpringSimpleMultiBean" init-method="initMethod">  
  15.            <property name="id" value="1"/>  
  16.            <property name="name" value="spring"/>  
  17.          </bean>  
  18.            
  19. <!--          <bean id="springOtherBean" class="org.study.spring.springinit.SpringOtherBean"></bean> -->  
  20.   
  21. </beans>  
SpringSimpleMultiBeanTest.java

[java]  view plain  copy
  1. package org.study.spring.springinit;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class SpringSimpleMultiBeanTest {  
  8.       
  9.       
  10.     @Test  
  11.     public void test2() throws Exception{  
  12.           
  13.          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-init.xml");  
  14.          SpringSimpleMultiBean bean = applicationContext.getBean("springMultiBean",SpringSimpleMultiBean.class);  
  15.          bean.say();  
  16.            
  17. //       SpringOtherBean springOtherBean = applicationContext.getBean("springOtherBean",SpringOtherBean.class);  
  18. //       springOtherBean.say();  
  19.     }  
  20.   
  21. }  
运行结果:




好了,我发现我不贴代码已经说明不了问题了,语言能力太差,我们先来看看结果吧~


①首先执行的是构造函数

②然后执行的BeanNameAware这个接口中的方法

③然后执行的是BeanFactoryAware这个接口中的方法

④执行InitializingBean接口中的afterPropertiesSet的方法

⑤执行我们在xml中定义的init-method这个方法

⑥最后执行的是BeanFactoryPostProcessor这个方法


这个执行结果我们可以清晰的了解spring的初始化流程,但你可以看出BeanPostProcessor并没有初始化,这是为什么呢?



我们再次打开BeanPostProcessor.java的源代码


翻译一下:应用这个BeanPostProcessor来返回一个新的bean的实例,当任何Bean回调的时候~


请注意是回调~


好吧,意思很明显,当其他非实现BeanPostProcessor的bean且在同一个容器的bean在初始化的时候,才会回调这个方法,所以这就理解了,为什么,我们这边没有执行的原因了,因为此时spring就管理了一个bean,没有其他的bean,何来的回调呢?


好吧,我们打开上面代码的SpringSimpleMultiBeanTest.java中17~18行的注释,spring-init.xml中19行的注释


好了,这次运行了,最后盗图一张



好了,spring的组件我们讲了好几个了,也讲了这些组件在spring初始化bean时候执行的顺序,下一节我们分析refresh()这个方法,应该会理解为什么会是这个执行顺序了,END~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值