spring中Bean的生命周期总结
spring中在ApplicationContext或在BeanFactory中Bean的生命周期总结
Spring中Bean的生命周期,在学习spring的过程中bean的生命周期理解对学习spring有很大的帮助,下面我就分别介绍在ApplicationContext和BeanFactory中Bean的生命周期。
1、在ApplicationContext中Bean的生命周期
生命周期执行的过程如下:
1、需找所有的bean根据bean定义的信息来实例化bean
2、使用依赖注入,spring按bean定义信息配置bean的所有属性
3、若bean实现了BeanNameAware接口,工厂调用Bean的setBeanName()方法传递bean的ID。
4、若bean实现了BeanFactoryAware接口,工厂调用setBeanFactory() 方法传入工厂自身。
5、若bean实现了ApplicationContextAware()接口,setApplicationContext()方法会被调用
6、若BeanPostProcessor和bean关联,
则它们的postProcessBeforeInitialization()方法被调用
7、若bean指定了init-method="init"方法、,它将被调用。
8、若有BeanPostProcessor和bean关联,
则它们的postProcessAfterInitialization()方法被调用
注意:通过已上操作,此时的Bean就可以被应用的系统使用,并讲保留在BeanFactory工厂中直到不再需要为止.但我们也可以通过9或者10进行销毁
9、若bean实现了DisposableBean接口,distroy()方法被调用
10、如果指定了destroy-method="close"定制的销毁方法,就调用这个方法
案例分析:
创建一个实体Bean代码如下:
- package www.csdn.spring.dao;
- 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;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ApplicationContextAware;
- public class HelloDaoImpl implements BeanNameAware, BeanFactoryAware,
- InitializingBean, ApplicationContextAware, DisposableBean {
- private String content;
- public HelloDaoImpl() {
- System.out.println("----------HelloDaoImpl实例化");
- }
- public void setContent(String content) {
- System.out.println("----------通过依赖注入的内容是:" + content);
- this.content = content;
- }
- @Override
- public void setBeanName(String beanId) {
- System.out.println("----------输出BeanId:" + beanId);
- }
- @Override
- public void setBeanFactory(BeanFactory factory) throws BeansException {
- System.out.println("----------factory:" + factory.getClass());
- }
- @Override
- public void setApplicationContext(ApplicationContext applicationContext)
- throws BeansException {
- System.out.println("----------" + applicationContext);
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("----------afterPropertiesSet");
- }
- public void init() {
- System.out.println("----------初始化方法");
- }
- @Override
- public void destroy() throws Exception {
- System.out.println("----------bean被销毁");
- }
- public void close() {
- System.out.println("----------close");
- }
- }
package www.csdn.spring.dao; 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; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class HelloDaoImpl implements BeanNameAware, BeanFactoryAware, InitializingBean, ApplicationContextAware, DisposableBean { private String content; public HelloDaoImpl() { System.out.println("----------HelloDaoImpl实例化"); } public void setContent(String content) { System.out.println("----------通过依赖注入的内容是:" + content); this.content = content; } @Override public void setBeanName(String beanId) { System.out.println("----------输出BeanId:" + beanId); } @Override public void setBeanFactory(BeanFactory factory) throws BeansException { System.out.println("----------factory:" + factory.getClass()); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("----------" + applicationContext); } @Override public void afterPropertiesSet() throws Exception { System.out.println("----------afterPropertiesSet"); } public void init() { System.out.println("----------初始化方法"); } @Override public void destroy() throws Exception { System.out.println("----------bean被销毁"); } public void close() { System.out.println("----------close"); } }
创建BeanPostProcessor实现类
- package www.csdn.spring.dao;
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.config.BeanPostProcessor;
- public class BeanPostProcessorImpl implements BeanPostProcessor {
- @Override
- public Object postProcessBeforeInitialization(Object bean, String beanName)
- throws BeansException {
- System.out.println("我把:" + beanName + "实例化化之前的操作");
- return bean;
- }
- @Override
- public Object postProcessAfterInitialization(Object bean, String beanName)
- throws BeansException {
- System.out.println("我把:" + beanName + "实例化化之后的操作");
- return bean;
- }
- }
package www.csdn.spring.dao; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class BeanPostProcessorImpl implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("我把:" + beanName + "实例化化之前的操作"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("我把:" + beanName + "实例化化之后的操作"); return bean; } }
在配置文件中配置实体Bean和BeanPostProcessorImpl 特殊的Bean
- <?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.xsd">
- <!-- spring容器 就是负责创建、管理、维护Bean 并且能够依赖注入到相应组件上 -->
- <bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl"
- scope="singleton" lazy-init="false" init-method="init" destroy-method="close">
- <!-- 通过set方法注入 -->
- <property name="content" value="陈老师早上好!" />
- </bean>
- <!-- BeanPostProcessor与当前的bean都进行关联 -->
- <bean class="www.csdn.spring.dao.BeanPostProcessorImpl" />
- </beans>
<?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.xsd"> <!-- spring容器 就是负责创建、管理、维护Bean 并且能够依赖注入到相应组件上 --> <bean id="helloDaoImpl" class="www.csdn.spring.dao.HelloDaoImpl" scope="singleton" lazy-init="false" init-method="init" destroy-method="close"> <!-- 通过set方法注入 --> <property name="content" value="陈老师早上好!" /> </bean> <!-- BeanPostProcessor与当前的bean都进行关联 --> <bean class="www.csdn.spring.dao.BeanPostProcessorImpl" /> </beans>
创建测试类BeanTest
- package www.csdn.spring.bean.cycle;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.ConfigurableApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class BeanTest {
- @Test
- public void test() {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "classpath:spring-dao.xml");
- // 强制造型
- ConfigurableApplicationContext cont = (ConfigurableApplicationContext) context;
- // 执行关闭 可以考到 destory-method的方法的执行
- cont.close();
- }
- }
package www.csdn.spring.bean.cycle; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:spring-dao.xml"); // 强制造型 ConfigurableApplicationContext cont = (ConfigurableApplicationContext) context; // 执行关闭 可以考到 destory-method的方法的执行 cont.close(); } }
执行结果如下:
总结:在ApplicationContext中Bean的生命周期
2、在 BeanFactory中Bean的生命周期
在BeanFactory中Bean的生命周期与ApplicationContext中bean的生命周期唯一不同的是:若bean实现了ApplicationContextAware()接口,在ApplicationContext中bean的生命周期中setApplicationContext()方法会被调用,而在BeanFactory中Bean的生命周期此方法不会被调用。
案例采用上个案例:测试类中测试代码如下:
- // 创建资源对象
- ClassPathResource resource = new ClassPathResource("spring-dao.xml");
- // 采用BeanFactory初始化容器
- ConfigurableBeanFactory cbf = new XmlBeanFactory(resource);
- // bean后置处理器必须通过addBeanPostProcessor(new BeanPostProcessorImpl())手动添加
- cbf.addBeanPostProcessor(new BeanPostProcessorImpl());
- //在配置文件中给BeanPostProcessorImpl的bean加上id="beanPostProcessorImpl" //cbf.addBeanPostProcessor(cbf.getBean("beanPostProcessorImpl", BeanPostProcessorImpl.class));
- // 获取Bean对象
- cbf.getBean("helloDaoImpl", HelloDaoImpl.class);
- // 销毁
- cbf.destroySingletons();
// 创建资源对象 ClassPathResource resource = new ClassPathResource("spring-dao.xml"); // 采用BeanFactory初始化容器 ConfigurableBeanFactory cbf = new XmlBeanFactory(resource); // bean后置处理器必须通过addBeanPostProcessor(new BeanPostProcessorImpl())手动添加 cbf.addBeanPostProcessor(new BeanPostProcessorImpl()); //在配置文件中给BeanPostProcessorImpl的bean加上id="beanPostProcessorImpl" //cbf.addBeanPostProcessor(cbf.getBean("beanPostProcessorImpl", BeanPostProcessorImpl.class)); // 获取Bean对象 cbf.getBean("helloDaoImpl", HelloDaoImpl.class); // 销毁 cbf.destroySingletons();
执行结果:
总结:在BeanFactory中Bean的生命周期
另外说明:
应用上下文与bean工厂的另一个重要区别是关于单例bean如何被加载。
bean工厂延迟加载所有bean,直到getBean()方法被调用。
应用上下文会在启动后预载入所有单例bean.这样可确保应用不需要等待他们被创建。