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. linux命令学习-1-less

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  2. iOS开发——打电话

    1.调用 自带mail [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://55522555 ...

  3. X-004 FriendlyARM tiny4412 uboot移植之点亮指路灯

    <<<<<<<<<<<<<<<<<<<<<<<<< ...

  4. Memcached源码分析之thread.c

    /* * 文件开头先啰嗦几句: * * thread.c文件代表的是线程模块.但是你会看到这个模块里面有很多其它方法, 例如关于item的各种操作函数,item_alloc,item_remove,i ...

  5. [转载] python利用psutil遍历进程名字和exe所在目录

    本文转载自: http://www.duanzhihe.com/1594.html http://www.jianshu.com/p/64e265f663f6 import psutil,os,tim ...

  6. VMWare虚拟机启动报错物理内存不足

    尝试了三种修改 1.说是微软补丁KB2995388冲突-->>失败 2.修改win8.1高级环境性能更改设置-->>失败 3.修改config.ini文件-->>成 ...

  7. Python知识小点(备注)

    (1)if __name__ == '__main__': 的作用是让后面的代码只有文件被作为程序执行时才有效,作为库加载时不执行

  8. wildfly 如何设置外网访问

    wildfly的默认配置是不支持外网访问的, 要想实现外网访问需要修改standalone.xml配置文件. 配置文件所在路径:wildfly/standalone/configuration/sta ...

  9. 添加Pods依赖

    1. 添加所需文件 1.1. 添加 .podspec 文件 1.1.1.  创建 必须文件 使用命令 pod spec create name.podspec 或者直接拷贝一份 1.1.2.  添加内 ...

  10. div+CSS实现段落首行缩进两个字符

    段落前面空两个字的距离,不要再使用空格了,用CSS实现段落首缩进两个字符.应该使用首行缩进text-indent.text-indent可以使得容器内首行缩进一定单位.比如中文段落一般每段前空两个汉字 ...