spring Bean的生命周期

bean的初始化函数

1.实现 InitializingBean 接口,容器会自动调用afterPropertiesSet();post

2. 指定 <bean id="typeResolver" class="" init-method=""></bean> 指定 一个方法给 init-methodspa

若是同时指定两种,先执行第一种,后再执行第二种。建议使用第2种,代码污染小.net

 

注解:xml

@Bean(initMethod="",destroyMethod = "") //至关于xml int-method接口

@PostConstruct        //在构造函数执行完成以后执行get

 

bean的销毁it

1.实现DisposableBean接口io

2.指定destroy-methodclass

注解:

@PreDestroy          //在bean 销毁以前执行

 

指定多个bean的初始化与销毁

1.能够在<beans>中指定default-init--method 属性

例: class User implent InitializingBean{

     void setName(){...}

     void init(){...}

     void afterPropertiesSet(){...}

}

Bean 后处理  是对全部的Bean进行统一处理,BeanProcessor 自己不对外提供服务,能够不设置id.容器将自动处理其中的方法

class BeanProcessor implent BeanPostProcessor{                                  

     Object postProcessBeforeInitializetion(bean,beanName){...}

     Object postProcessAfterInitialization(bean,beanName){...}

}

 

1. 执行  User  中 setName  完依赖注入  

 

2.  执行 BeanProcessor 中 postProcessBeforeInitializetion方法处理

 

3. 执行  User  中  afterPropertiesSet  

 

4. 执行  User  中 init  配置<bean init-method="int">

 

5.  执行 BeanProcessor 中 postProcessAfterInitialization

相关文章
相关标签/搜索