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的生命周期的更多相关文章

  1. spring深入学习(二)-----bean的生命周期、IOC容器bean装配

    bean的生命周期 1.实例化Bean对于BeanFactory容器,当客户向容器请求一个尚未初始化的bean时,或初始化bean的时候需要注入另一个尚未初始化的依赖时,容器就会调用createBea ...

  2. Spring系列13:bean的生命周期

    本文内容 bean的完整的生命周期 生命周期回调接口 Aware接口详解 Spring Bean的生命周期 面试热题:请描述下Spring的生命周期? 4大生命周期 从源码角度来说,简单分为4大阶段: ...

  3. spring IOC 容器中 Bean 的生命周期

    IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...

  4. MyEclipse Spring 学习总结二 Bean的生命周期

    文件结构可以参考上一节 Bean的生命周期有方法有:init-method,destroy-method ApplicationContext.xml 文件配置如下: <?xml version ...

  5. Spring IOC容器中Bean的生命周期

    1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...

  6. Spring学习记录(八)---Bean的生命周期

    之前说过,在调用下面时,就创建了容器和对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml&quo ...

  7. Spring IOC容器对bean的生命周期进行管理的过程

    1.通过构造器或者工厂方法创建bean的实例 2.为bean的属性设置值和对其他bean的引用 3.将bean的实例传递给bean的后置处理器BeanPostProcessor的postProcess ...

  8. Spring《二》 Bean的生命周期

    Bean初始化 1.bean中实现public void init():方法,config.xml中增加init-method="init" 属性. 2.bean实现接口Initi ...

  9. Spring Bean的生命周期,《Spring 实战》书中的官方说法

    连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...

随机推荐

  1. Android L(5.0)源码之图形与图像处理之简单图片——Bitmap

    最近在研究android 5.0的gallery模块,学习了相关的知识点,准备写点博客总结一下,有时间了会补充完整

  2. UILabel常用属性小结

    标签常用的属性: (1)frame属性:设置标签的位置与大小. frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heig ...

  3. 部分网站允许空白referer的防盗链图片的js破解代码

    Reference: http://www.114390.com/article/27125.htm Javascript源码: 复制代码代码如下: function showImg( url ) { ...

  4. thinkPHP的学习

    1.版本,以3.1为主,因为手册是基于这个的,最新的版本,还没有对应的手册 2.发现一个问题,echo 中文时,出现乱码,而调用模版则正常. 3.写url的注意大小写.index和Index是不同的 ...

  5. Mysql和Oracle的一些语法区别

    作为一个有追求的程序猿,当然要不断的学习,巴拉巴拉巴拉...好了,贴一个网址给大家,哈哈 MySQL与Oracle 差异比较:http://www.cnblogs.com/HondaHsu/p/364 ...

  6. 学习笔记::LCT

    今天听见茹大神20分钟讲完了LCT,10分钟讲完平衡树,5分钟讲完树剖,感觉自己智商还不及他一半... 还有很多不懂:2017/1/15 的理解: access是干什么用的? 不知道,只知道他是用来把 ...

  7. bzoj3991 [Sdoi2015]寻宝游戏 set动态维护虚树+树链求并

    题目大意:支持多次操作,增加或删除一个关键点 动态维护虚树边权和*2 分析:可以用树链求并的方法,最后减去虚树的根到1距离 注意到树链求并是所有点到根距离-所有dfn序相邻两点的LCA到根距离 找df ...

  8. php模式设计之 中介者模式

    中介者模式 中介者模式用于开发一个对象,这个对象能够在类似对象相互之间不直接相互的情况下传送或者调解对这些对象的集合的修改.一般处理具有类似属性,需要保持同步的非耦合对象时,最佳的做法就是中介者模式. ...

  9. PHP导入导出Excel方法

    看到这篇文章的时候,很是惊讶原作者的耐心,虽然我们在平时用的也 有一些,但没有作者列出来的全,写excel的时候,我用过pear的库,也用过pack压包的头,同样那些利用smarty等作的简单替换xm ...

  10. MYBATIS 无效的列类型: 1111

    查询的时候竟然也会报错,如果参数是数字,需要加上jdbcType 在xml中加上 t.chart_id = #{chartId,jdbcType=DECIMAL}