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. UIAlertView使用全解

    举例: UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Default Alert View"messa ...

  2. 最新 Spring 4.2.2 集成 Quartz Scheduler 2.2.2 任务调度示例

    参考http://blog.csdn.net/defonds/article/details/49496895 本文将演示如何通过 Spring 使用 Quartz Scheduler 进行任务调度. ...

  3. JQuery的 jQuery.fn.extend() 和jQuery.extend();

    原文链接:http://caibaojian.com/jquery-extend-and-jquery-fn-extend.html jQuery.fn.extend(); jQuery.extend ...

  4. thinkPHP的学习

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

  5. StackExchange.Redis 官方文档(四) KeysScan

    KEYS, SCAN, FLUSHDB 方法在哪? 经常有人问这些问题: 好像并没有看到 Keys(...) 或者 Scan(...)方法?那我要怎么查询数据库里面存有哪些key? 或者 好像没有Fl ...

  6. Linux之目录基本操作命令

    Linux之目录基本操作命令 目录基本操作命令 1.tree命令 tree命令以树状图列出目录的内容. 语法 tree(选项)(参数) 选项 1.-a显示所有文件和目录 2.-A使用ASNI绘图字符显 ...

  7. JavaWeb:EL & JSTL

    EL:全名为 Expression Language 1.语法:${sessionScope.user.sex}(从Session 的范围中,取得用户的性别), 所有的EL 都是以 ${  为起始,以 ...

  8. reg 正则

    //转化为camel形式 var text = 'border-color-base'; text.replace(/-(\w{1})/g, function (match, chr1) { retu ...

  9. samba.conf 范例

    # Sample configuration file for the Samba suite for Debian GNU/Linux. # # This is the main Samba con ...

  10. referrer vs referer

    http request里面是referer 其实是http规范拼写错了,正确的拼写应该是referrer