LifeCycle类:
package com.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class LifeCycle implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
public LifeCycle() {
System.out.println("Constructor executing");
}
private String cycle;
public void setCycle(String cycle) {
System.out.println("Setter methods executing");
this.cycle = cycle;
}
@Override
public void setBeanName(String beanName) {
System.out.println("BeanNameAware.setBeanName(String beanName) executing");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware.setBeanFactory(BeanFactory beanFactory) executing");
}
@PostConstruct
public void beforeBeanConstructed() {
System.out.println("BeanPostProcessor executing (before bean constructed)");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean.afterPropertiesSet() executing");
}
public void initMethod() {
System.out.println("InitMethod executing");
}
@PreDestroy
public void afterBeanConstructed() {
System.out.println("BeanPostProcessor executing (after bean constructed)");
}
@Override
public void destroy() throws Exception {
System.out.println("DisposableBean.destroy() executing");
}
public void destroyMethod() {
System.out.println("DestroyMethod executing");
}
}
配置文件beans-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="lifeCycle" class="com.bean.LifeCycle" init-method="initMethod" destroy-method="destroyMethod">
<property name="cycle" value="Life Cycle"/>
</bean>
<bean id="beanPostProcessor" class="org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor">
<property name="initAnnotationType" value="javax.annotation.PostConstruct"/>
<property name="destroyAnnotationType" value="javax.annotation.PreDestroy"/>
</bean>
</beans>
测试类:
package com.action;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.bean.LifeCycle;
public class LifeCycleDemo {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("beans-config.xml"));
BeanPostProcessor bpp = (BeanPostProcessor)factory.getBean("beanPostProcessor");
factory.addBeanPostProcessor(bpp);
LifeCycle lifeCycle = (LifeCycle)factory.getBean("lifeCycle");
factory.destroySingleton("lifeCycle");
}
}
/**Output:
Constructor executing
Setter methods executing
BeanNameAware.setBeanName(String beanName) executing
BeanFactoryAware.setBeanFactory(BeanFactory beanFactory) executing
BeanPostProcessor executing (before bean constructed)
InitializingBean.afterPropertiesSet() executing
InitMethod executing
BeanPostProcessor executing (after bean constructed)
DisposableBean.destroy() executing
DestroyMethod executing
*/
//解释:
在传统的Java应用中,Bean的生命周期非常简单。
Java的关键词new用来实例化Bean(或许他是非序列化的)。这样就够用了。
相反,Bean的生命周期在Spring容器中更加细致。
理解Spring Bean的生命周期非常重要,因为你或许要利用Spring提供的机会来订制Bean的创建过程。
1.容器寻找Bean的定义信息并且将其实例化。
2.执行依赖注入,Spring按照Bean定义信息注入Bean的所有属性。
3.如果Bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法,并传递Bean的ID作为参数。
4.如果Bean实现了BeanFactoryAware接口,工厂调用setBeanFactory()方法传入工厂自身。
5.如果BeanPostProcessor和Bean关联,那么它们的postProcessBeforeInitialzation()方法将被调用。
6.如果Bean实现了InitializingBean接口,afterPropertiesSet()方法会被调用;
如果Bean指定了init-method属性,init-method属性指定的方法将被调用。
如果InitializingBean和init-method属性同时出现,前者先于后者执行。
7.如果有BeanPsotProcessor和Bean关联,那么它们的postProcessAfterInitialization()方法将被调用。
到这个时候,Bean已经可以被应用系统使用了,并且将被保留在Bean Factory中直到它不再需要。
8.最后当关闭应用时,Bean Factory会调用destroySingletons()方法,这时,会执行:
如果Bean实现了DisposableBean接口,destory()方法会被调用;
如果指定了destroy-method属性,destroy-method属性指定的方法将被调用。
如果DisposableBean和destroy-method属性同时出现,前者先于后者执行。
Bean在Spring应用上下文的生命周期与在Bean工厂中的生命周期只有一点不同,
唯一不同的是,如果Bean实现了ApplicationContextAwre接口,setApplicationContext()方法被调用。
只有singleton行为的bean接受容器管理生命周期。
non-singleton行为的bean,Spring容器仅仅是new的替代,容器只负责创建。
分享到:
相关推荐
Spring bean生命周期demo
spring bean 的生命周期,把运行结果的日志,用sublime打开对比查看,你会有比较清晰的认识
Springbean生命周期
Spring Bean 生命周期之“我从哪里来?”
Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解。这篇文章主要介绍了Spring Bean 生命周期,需要的朋友可以参考下
虽然可以随意配置 <bean> 的属性,但是建议不要过多地使 Bean 实现接,因为这样会导致代码和 Spring 的聚合过于紧密第 1 步:实例化Bean第
SpringBean的生命周期.mdj
本篇文章主要介绍了浅谈Spring bean 生命周期验证,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这个工程主要实现了: Spring中Bean的生命周期 applicationcontext的应用(实现国际化,事件的传递)
主要介绍了Spring bean生命周期配置过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
此资源是我的博客bean的生命周期的测试代码,只有源代码,没有相关库文件,环境是spring4.2 ,
主要介绍了Spring Bean的生命周期的相关资料,需要的朋友可以参考下
本文主要介绍 Spring IoC 容器如何管理 Bean 的生命周期。 在应用开发中,常常需要执行一些特定的初始化工作,这些工作都是相对比较固定的,比如建立数据库连接,打开网络连接等,同时,在结束服务时,也有一些相对...
主要给大家介绍了Spring中Bean的生命周期和作用域及实现方式的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
介绍了Spring的Bean周期,容器周期,工厂周期,运行代码后可以看到运行结果
本篇文章主要介绍了spring之Bean的生命周期详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本篇文章主要介绍了深入理解Spring中bean的生命周期介绍,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。Bean的生命周期从Spring容器实例化Bean到销毁Bean。 本文分别对 BeanFactory 和 ...
学习Spring过程中,使用Eclipse调试Spring源码的关键断点文件。