Spring Bean 初始化过程

Spring Bean 初始化过程

分类: Java技术2008-11-07 09:43 8793人阅读 评论(3) 收藏 举报

springbeanuserstringobjectencoding

Spring 的几个接口

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

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

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

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

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

 

下面来看一下例子

1.  package com.test;

2.   

3.  public class User {

4.      private String name;

5.      

6.      public String getName() {

7.          return name;

8.      }

9.   

10.     public void setName(String name) {

11.         System.out.println("User setName方法");

12.         this.name = name;

13.     }

14.     public void init() throws Exception {

15.         System.out.println("Userinit 方法");

16.         

17.     }

18.     public User(){

19.         System.out.println("User类构造方法");

20.     }

21.     

22. }

23.  

 

1.  package com.test;

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.DisposableBean;

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 JavaBean implements InitializingBean,DisposableBean,BeanFactoryAware,BeanPostProcessor ,BeanFactoryPostProcessor{

13.     private String name;

14.     public JavaBean(){

15.         System.out.println("JavaBean类构造方法");

16.     }

17.     @Override

18.     public void afterPropertiesSet() throws Exception {

19.         System.out.println("InitializingBean 接口 afterPropertiesSet方法");

20.         

21.     }

22.  

23.     @Override

24.     public void destroy() throws Exception {

25.         System.out.println("DisposableBean 接口 destroy方法");

26.         

27.     }

28.     public void init() throws Exception {

29.         System.out.println("JavaBeaninit 方法");

30.         

31.     }

32.  

33.     @Override

34.     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

35.         System.out.println("BeanFactoryAware 接口 setBeanFactory方法");

36.         

37.     }

38.  

39.     @Override

40.     public Object postProcessAfterInitialization(Object bean, String beanName)

41.             throws BeansException {

42.         System.out.println(beanName+":BeanPostProcessor 接口 postProcessAfterInitialization方法");

43.         return bean;

44.     }

45.  

46.     @Override

47.     public Object postProcessBeforeInitialization(Object bean, String beanName)

48.             throws BeansException {

49.         System.out.println(beanName+":BeanPostProcessor 接口 postProcessBeforeInitialization 方法");

50.         return bean;

51.     }

52.  

53.     public String getName() {

54.         return name;

55.     }

56.  

57.     public void setName(String name) {

58.         System.out.println("JavaBeansetName 方法");

59.         this.name = name;

60.     }

61.     @Override

62.     public void postProcessBeanFactory(

63.             ConfigurableListableBeanFactory beanFactory) throws BeansException {

64.         System.out.println("BeanFactoryPostProcessor 接口 postProcessBeanFactory 方法");

65.     }

66.     

67.  

68. }

69.  

 

1.  package com.test;

2.   

3.  import org.springframework.context.ApplicationContext;

4.  import org.springframework.context.support.ClassPathXmlApplicationContext;

5.   

6.  public class Test {

7.   

8.      /**

9.       * @param args

10.      */

11.     public static void main(String[] args) {

12.         System.out.println("加载Spring配置文件");

13.          ApplicationContext context =  

14.                 new ClassPathXmlApplicationContext("spring.xml"); 

15.          System.out.println("加载Spring配置文件结束");

16.          JavaBean bean=(JavaBean)context.getBean("javaBean");

17.          System.out.println("获取name属性:"+bean.getName());

18.          System.out.println("程序结束");

19.     }

20.  

21. }

22.  

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方法,目前还不知道原因在什么地方

分享到: 

·  上一篇:使用 CXF Spring 创建 Web 服务

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值