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. IFrame跨域访问自定义高度

    由于JS禁止跨域访问,如何实现不同域的子页面将高度返回给父页面本身,是解决自定义高度的难点. JS跨域访问问题描述:应用A访问应用B的资源,由于A,B应用分别部署在不同应用服务器(tomcat)上,属 ...

  2. Golang测试技术

    本篇文章内容来源于Golang核心开发组成员Andrew Gerrand在Google I/O 2014的一次主题分享“Testing Techniques”,即介绍使用Golang开发 时会使用到的 ...

  3. easyui-01 怎么样使用easyui

    console.info();在控制台打印. 1.引入 <script type="text/javascript" src="../../jquery-easyu ...

  4. html 上传文件

    1.html代码 <form id="form1" action="TestYield" method="post" enctype= ...

  5. 【推荐】PHP中格式化时间函数date与gmdate的区别 | 修改PHP的默认时区

    PHP中的时间有2个格式化函数:date()和gmdate(),在官方的文档中的描述为: date -- 格式化一个本地时间/日期 gmdate -- 格式化一个 GMT/UTC 日期/时间,返回的是 ...

  6. 华硕A450c详细清灰拆机教程

    很久都想写点东西,但又无从下笔. 上次把自己的笔记本清了灰,这次有时间就整理一下,随便作为我的第一次随笔. 准备:笔记本(我的是华硕A450c),拆机工具(螺丝刀等) 温馨提示:要慢点 1,先翻开笔记 ...

  7. OC--类型为ID 的类的名称

    NSString *str = [[view class] description];

  8. javac不是内部或外部命令

    1.描述 在命令行输入javac,提示“不是内部或外部命令”. 2.解决过程 2.1.解决方案一 2.1.1.检查并添加环境变量 通常就是这个原因导致. 2.1.2.过程一 确实还是环境变量没有写对. ...

  9. Android应用性能优化方案

    1.避免创建不必要的对象 2.如果方法用不到成员变量,可以把方法声明为静态(static),这样性能会提高百分之十五到百分之二十 3.避免使用get/set存取字段,可以把字段声明为public直接访 ...

  10. Repeated Substring Pattern Leetcode

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...