2007-03-27
如果监控Spring Bean生命周期
对于singleton bean,Spring容器知道bean何时实例化结束,何时销毁,Spring可以管理实例化结束之后,和销毁之前的行为,管理bean的生命周期行为主要未如下两个时机:
Bean全部依赖注入之后
Bean即将销毁之前
(1)依赖关系注入后的行为实现:
有两种方法:A.编写init方法 B.实现InitializingBean接口
afterPropertiesSet和init同时出现,前者先于后者执行,使用init方法,需要对配置文件加入init-method属性

public void init()...{
System.out.println("in init");
}

public void afterPropertiesSet() throws Exception ...{
System.out.println("in afterPropertiesSet");
}
<bean id="chinese" class="Bean.lifecycle.Chinese" init-method="init" destroy-method="close">
<property name="axe">
<ref local="axe"/>
</property>
</bean>
(2)bean销毁之前的行为
有两种方法:A.编写close方法 B.实现DisposableBean接口
destroy和close同时出现,前者先于后者执行,使用close方法,需要对配置文件加入destroy-method属性

public void close()...{
System.out.println("in close");
}
public void destroy() throws Exception ...{
System.out.println("in destroy");
}
<bean id="chinese" class="Bean.lifecycle.Chinese" init-method="init" destroy-method="close">
<property name="axe">
<ref local="axe"/>
</property>
</bean>
如果需要在bean创建之时和创建之后进行监控,则需要实现BeanPostProcessor接口
其中有两个方法:postProcessBeforeInitialization和postProcessAfterInitialization
这两个方法和init方法的顺序是:postProcessBeforeInitialization-->init-->postProcessAfterInitialization
发表评论
- 浏览: 84483 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Static初始化代码快访问st ...
当然啦,final型常量只能被赋值一次
-- by Chihom -
Tomcat 5.5不能使用EL表达 ...
請在web.xml修改XSD<?xml version="1.0" enc ...
-- by trowa -
一个通用的泛型GenericH ...
谢谢!使用中!
-- by sangood -
Spring AOP四种创建通知( ...
我一直想知道如何拦截某个类中特定的方法,而不是所有的方法?
-- by yourgame -
HashTable和HashMap的区 ...
这么多总结,还算你的这个不错,谢谢,学习
-- by lysmart_8






评论排行榜