Spring应用上下文中Bean的生命周期
Bean装载到Spring应用上下文的生命周期,如图:
Bean在Spring容器中从创建到销毁经历了若干个阶段,每一阶段都可以对Spring如何管理Bean进行个性化定制,以下我们通过代码去验证生命周期以及个性化定制方法;
BeanLife实现Aware接口、InitializingBean、DisposableBean接口,自定义生命周期中的方法。
/**
* @name Bean生命周期
*/
public class BeanLife implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,DisposableBean{ private String beanProperty;//私有属性 private String beanName; //接收BeanNameAware的beanName传入
private BeanFactory beanFactory;//接收BeanFactoryAware的beanFactory传入
private ApplicationContext applicationContext;//接收ApplicationContextAware的applicationContext传入 /**
* 构造函数
*/
public BeanLife(String beanProperty){
System.out.println("BeanLife constructed with "+beanProperty+"......");
} /**
* bean属性get
*/
public String getBeanProperty() {
return beanProperty;
} /**
* bean填充属性
*/
public void setBeanProperty(String beanProperty) {
System.out.println("BeanLife setBeanProperty:"+beanProperty);
this.beanProperty = beanProperty;
} /**
* init-method关联方法
*/
public void begin(){
System.out.println("init-method:begin()");
}
/**
* destroy-method关联方法
*/
public void end(){
System.out.println("destroy-method:end()");
} /**
* 准备就绪的bean,使用Bean做些事情
*/
public void doSomething(){
System.out.println("BeanLife can be used: do something.");
} /**
* Spring将BeanFactory容器实例传入
*/
public void setBeanFactory(BeanFactory arg0) throws BeansException {
this.beanFactory=arg0;
System.out.println("BeanFactoryAware---BeanLife setBeanFactory:"+this.beanFactory.toString());
} /**
* Spring将Bean的ID传入
*/
public void setBeanName(String arg0) {
this.beanName = arg0;
System.out.println("BeanNameAware---BeanLife setBeanName:"+this.beanName);
} /**
* Spring将应用上下文的引用传入
*/
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
this.applicationContext = arg0;
System.out.println("ApplicationContextAware---BeanLife setApplicationContext:"+this.applicationContext.getApplicationName());
} /**
* 属性设置完毕后
*/
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean---After SetProperties");
}
/**
* Bean销毁
*/
public void destroy() throws Exception {
System.out.println("DisposableBean---BeanLife Bean destroy.");
}
}
自定义一个BeanPostProcessor进行监控Spring容器Bean实例化注入时的回调方法定制
/**
* @name BeanPostProcessor实现
*/
public class BeanLifePostProcessor implements BeanPostProcessor{ /**
* 实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务
* @param arg0 Bean对象
* @param arg1 Bean的ID
*/
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("BeanPostProcessor---Before "+arg1+"'s Initialization ");
return arg0;
} /**
* 实例化、依赖注入、初始化完毕时执行
* @param arg0 Bean对象
* @param arg1 Bean的ID
*/
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException { System.out.println("BeanPostProcessor---After "+arg1+"'s Initialization");
return arg0;
} }
applicationContext.xml配置
<!-- BeanLife -->
<bean id="beanlife" class="com.wjx.betalot.BeanLife" init-method="begin" destroy-method="end">
<constructor-arg value="beanlife construct param"></constructor-arg>
<property name="beanProperty" value="beanlife cycle"></property>
</bean>
<!-- beanPostProcessor -->
<bean id="beanPostProcessor" class="com.wjx.betalot.BeanLifePostProcessor"></bean>
验证测试:
public static void main( String[] args ) throws Exception{ ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
BeanLife beanlife = (BeanLife) context.getBean("beanlife");
beanlife.doSomething();
//销毁bean
((ClassPathXmlApplicationContext)context).registerShutdownHook();
}
测试结果:
BeanLife constructed with beanlife construct param......
BeanLife setBeanProperty:beanlife cycle
BeanNameAware---BeanLife setBeanName:beanlife
BeanFactoryAware---BeanLife setBeanFactory:org.springframework.beans.factory.support.DefaultListableBeanFactory@5eed2fce: defining beans [beanlife,beanPostProcessor]; root of factory hierarchy
ApplicationContextAware---BeanLife setApplicationContext:
BeanPostProcessor---Before beanlife's Initialization
InitializingBean---After SetProperties
init-method:begin()
BeanPostProcessor---After beanlife's Initialization
BeanLife can be used: do something.
DisposableBean---BeanLife Bean destroy.
destroy-method:end()
可对照Bean的生命周期图,进行验证。
Spring应用上下文中Bean的生命周期的更多相关文章
- spring深入学习(二)-----bean的生命周期、IOC容器bean装配
bean的生命周期 1.实例化Bean对于BeanFactory容器,当客户向容器请求一个尚未初始化的bean时,或初始化bean的时候需要注入另一个尚未初始化的依赖时,容器就会调用createBea ...
- Spring系列13:bean的生命周期
本文内容 bean的完整的生命周期 生命周期回调接口 Aware接口详解 Spring Bean的生命周期 面试热题:请描述下Spring的生命周期? 4大生命周期 从源码角度来说,简单分为4大阶段: ...
- spring IOC 容器中 Bean 的生命周期
IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...
- MyEclipse Spring 学习总结二 Bean的生命周期
文件结构可以参考上一节 Bean的生命周期有方法有:init-method,destroy-method ApplicationContext.xml 文件配置如下: <?xml version ...
- Spring IOC容器中Bean的生命周期
1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...
- Spring学习记录(八)---Bean的生命周期
之前说过,在调用下面时,就创建了容器和对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml&quo ...
- Spring IOC容器对bean的生命周期进行管理的过程
1.通过构造器或者工厂方法创建bean的实例 2.为bean的属性设置值和对其他bean的引用 3.将bean的实例传递给bean的后置处理器BeanPostProcessor的postProcess ...
- Spring《二》 Bean的生命周期
Bean初始化 1.bean中实现public void init():方法,config.xml中增加init-method="init" 属性. 2.bean实现接口Initi ...
- Spring Bean的生命周期,《Spring 实战》书中的官方说法
连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...
随机推荐
- 你会做Web上的用户登录功能吗?
Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能后,我觉得很有必要写一篇文章教大家怎么来做用户登录功能.下面的文章告诉大家这个功能可能并没有你所想像的那么简单,这是一个关 ...
- easyui-01 怎么样使用easyui
console.info();在控制台打印. 1.引入 <script type="text/javascript" src="../../jquery-easyu ...
- fastcgi_param 详解
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#脚本文件请求的路径 fastcgi_param QUERY_STRI ...
- java 获取指定日期
//可以设置指定那一天:例如,最近一周,参数传入-7,最近一月,参数传入-30...private String getBeginDate(int date) throws ParseExceptio ...
- UVa 10299 - Relatives
题目大意:Euler's Totient的应用. 几乎和UVa 10179 - Irreducable Basic Fractions一样,于是偷了个懒,直接用10179题的代码,结果WA了,感觉一样 ...
- 那些年我们一起改过的bug
ORA-01861: 文字与格式字符串不匹配 ORA-00936: 缺失表达式 ORA-01810 格式代码出现两次 ORA-01722: 无效数字 无效的列索引
- mvn常用指令记录
maven工程版本号更新: -------------------------------------------------------------------------------------- ...
- KERNEL32相关函数
CALL DWord Ptr [<&KERNEL32.WriteFile>] kernel32.WriteFile 将数据写入一个文件,也可将这个函数应用于对通信设备.管道.套接字 ...
- js原生设计模式——7原型模式之真正的原型模式——对象复制封装
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Struts2的拦截器----Dog实例
拦截器是一个类,这个类包含方法,用来解决DRY规则,即代码复用的问题.如果不调用拦截器,代码中需要显示通过代码调用目标方法,定义了拦截器,系统就会自动执行.大部分时候,拦截器方法都是通过代理的方式调用 ...