Spring Bean 初始化过程

转:http://blog.csdn.net/zhjb1025/article/details/3244728

Spring 的几个接口

1.InitializingBean接口,在初始化Bean时容器会调用前者的afterPropertiesSet()方法

2.DisposableBean接口,在析构Bean时容器会调用destroy()方法,在下面的例子中好像没有体现出来(革命尚未成功,同志仍需努力)

3.BeanFactoryAware接口,当它被BeanFactory创建后,它会拥有一个指向创建它的BeanFactory的引用

4.BeanPostProcessor接口,这个接口两个方法,postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName) 在其他Bean构造前后执行

5.BeanFactoryPostProcessor接口,Spring IoC容器允许BeanFactoryPostProcessor在容器实际实例化任何其它的bean之前读取配置元数据,并有可能修改它

 

下面来看一下例子

  1. package com.test;
  2. public class User {
  3.     private String name;
  4.     
  5.     public String getName() {
  6.         return name;
  7.     }
  8.     public void setName(String name) {
  9.         System.out.println("User类 setName方法");
  10.         this.name = name;
  11.     }
  12.     public void init() throws Exception {
  13.         System.out.println("User类init 方法");
  14.         
  15.     }
  16.     public User(){
  17.         System.out.println("User类构造方法");
  18.     }
  19.     
  20. }

 

  1. package com.test;
  2. import org.springframework.beans.BeansException;
  3. import org.springframework.beans.factory.BeanFactory;
  4. import org.springframework.beans.factory.BeanFactoryAware;
  5. import org.springframework.beans.factory.DisposableBean;
  6. import org.springframework.beans.factory.InitializingBean;
  7. import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
  8. import org.springframework.beans.factory.config.BeanPostProcessor;
  9. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  10. public class JavaBean implements InitializingBean,DisposableBean,BeanFactoryAware,BeanPostProcessor ,BeanFactoryPostProcessor{
  11.     private String name;
  12.     public JavaBean(){
  13.         System.out.println("JavaBean类构造方法");
  14.     }
  15.     @Override
  16.     public void afterPropertiesSet() throws Exception {
  17.         System.out.println("InitializingBean 接口 afterPropertiesSet方法");
  18.         
  19.     }
  20.     @Override
  21.     public void destroy() throws Exception {
  22.         System.out.println("DisposableBean 接口 destroy方法");
  23.         
  24.     }
  25.     public void init() throws Exception {
  26.         System.out.println("JavaBean类init 方法");
  27.         
  28.     }
  29.     @Override
  30.     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
  31.         System.out.println("BeanFactoryAware 接口 setBeanFactory方法");
  32.         
  33.     }
  34.     @Override
  35.     public Object postProcessAfterInitialization(Object bean, String beanName)
  36.             throws BeansException {
  37.         System.out.println(beanName+":BeanPostProcessor 接口 postProcessAfterInitialization方法");
  38.         return bean;
  39.     }
  40.     @Override
  41.     public Object postProcessBeforeInitialization(Object bean, String beanName)
  42.             throws BeansException {
  43.         System.out.println(beanName+":BeanPostProcessor 接口 postProcessBeforeInitialization 方法");
  44.         return bean;
  45.     }
  46.     public String getName() {
  47.         return name;
  48.     }
  49.     public void setName(String name) {
  50.         System.out.println("JavaBean类setName 方法");
  51.         this.name = name;
  52.     }
  53.     @Override
  54.     public void postProcessBeanFactory(
  55.             ConfigurableListableBeanFactory beanFactory) throws BeansException {
  56.         System.out.println("BeanFactoryPostProcessor 接口 postProcessBeanFactory 方法");
  57.     }
  58.     
  59. }

 

  1. package com.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test {
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         System.out.println("加载Spring配置文件");
  10.          ApplicationContext context =  
  11.                 new ClassPathXmlApplicationContext("spring.xml"); 
  12.          System.out.println("加载Spring配置文件结束");
  13.          JavaBean bean=(JavaBean)context.getBean("javaBean");
  14.          System.out.println("获取name属性:"+bean.getName());
  15.          System.out.println("程序结束");
  16.     }
  17. }

Spring配置文件

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4.     <bean id="javaBean" class="com.test.JavaBean" init-method="init">
  5.         <property name="name" value="zhjb"></property>
  6.     </bean>
  7.     <bean id="user" class="com.test.User" init-method="init">
  8.         <property name="name" value="zhjb" ></property>
  9.     </bean>
  10. </beans>

运行结果如下:

加载Spring配置文件
JavaBean类构造方法
JavaBean类setName 方法
BeanFactoryAware 接口 setBeanFactory方法
InitializingBean 接口 afterPropertiesSet方法
JavaBean类init 方法
BeanFactoryPostProcessor 接口 postProcessBeanFactory 方法
User类构造方法
User类 setName方法
user:BeanPostProcessor 接口 postProcessBeforeInitialization 方法
User类init 方法
user:BeanPostProcessor 接口 postProcessAfterInitialization方法
加载Spring配置文件结束
获取name属性:zhjb
程序结束

 

从结果进行分析,Bean的初始化顺序应该是

1.构造函数

2.初始化属性

3.如果实现了BeanFactoryAware 接口执行setBeanFactory方法

4..如果实现了InitializingBean 接口执行afterPropertiesSet方法

5.如果在配置文件中指定了init-method,那么执行该方法

6..如果实现了BeanFactoryPostProcessor 接口在 “new”其他类之前执行 postProcessBeanFactory 方法(通过这个方法可以改变配置文件里面的属性值的配置)

7.如果实现了BeanFactoryPostProcessor 接口,那么会在其他bean初始化方法之前执行postProcessBeforeInitialization 方法,之后执行postProcessAfterInitialization方法

感觉奇怪的地方就是没有执行destroy方法,目前还不知道原因在什么地方

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值