在传统的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指定了init-method方法,它将被调用。
7.最后,如果有BeanPsotProcessor和Bean关联,那么它们的postProcessAfterInitialization()方法将被调用。
到这个时候,Bean已经可以被应用系统使用了,并且将被保留在Bean Factory中知道它不再需要。有两种方法可以把它从Bean Factory中删除掉。
1.如果Bean实现了DisposableBean接口,destory()方法被调用。
2.如果指定了订制的销毁方法,就调用这个方法。
Bean在Spring应用上下文的生命周期与在Bean工厂中的生命周期只有一点不同,唯一不同的是,如果Bean实现了ApplicationContextAwre接口,setApplicationContext()方法被调用。
举例:(这里只是演示Bean工厂的例子)
我这里用的是spring-1.2.6,由于版本原因吧,这里演示的不是很好!
1.HelloWorld.java
package com.spring.lifecycle; 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.beans.factory.config.BeanPostProcessor; public class HelloWorld implements BeanNameAware,BeanFactoryAware, BeanPostProcessor,InitializingBean,DisposableBean{ private String hello; public void setBeanName(String arg0) { System.out.println("调用BeanNameAware的setBeanName()..." ); } public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; System.out.println("调用setHello()..." ); } public void customInit() { System.out.println("调用customInit()..."); } public void customDestroy() { System.out.println("调用customDestroy()..."); } public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { System.out.println("调用BeanPostProcessor的postProcessAfterInitialization()..."); return null; } public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { System.out.println("调用BeanPostProcessor的postProcessBeforeInitialization()..."); return null; } public void destroy() throws Exception { System.out.println("调用DisposableBean的destory()..."); } public void afterPropertiesSet() throws Exception { System.out.println("调用InitializingBean的afterPropertiesSet()..."); } public void setBeanFactory(BeanFactory arg0) throws BeansException { System.out.println("调用BeanFactoryAware的setBeanFactory()..."); } }
2.测试程序
package com.spring.springtest; import junit.framework.TestCase; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.lifecycle.HelloWorld; public class TestLifeCycle extends TestCase { private BeanFactory bf; protected void setUp() throws Exception { bf = new ClassPathXmlApplicationContext("applicationContext.xml"); } public void testLifeCycle() throws Exception { HelloWorld hello = (HelloWorld) bf.getBean("helloWorld"); assertEquals("hello world!", hello.getHello()); hello.destroy(); } }
3.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloWorld" class="com.spring.lifecycle.HelloWorld" init-method="customInit" destroy-method="customDestroy"> <property name="hello" value="hello world!"></property> </bean> </beans>
4.运行结果:
调用BeanNameAware的setBeanName()...
调用BeanFactoryAware的setBeanFactory()...
调用InitializingBean的afterPropertiesSet()...
调用customInit()...
调用DisposableBean的destory()...
相关推荐
spring bean 的生命周期,把运行结果的日志,用sublime打开对比查看,你会有比较清晰的认识
Spring bean生命周期demo
SpringBean的生命周期.mdj
Springbean生命周期
这个工程主要实现了: Spring中Bean的生命周期 applicationcontext的应用(实现国际化,事件的传递)
此资源是我的博客bean的生命周期的测试代码,只有源代码,没有相关库文件,环境是spring4.2 ,
本篇文章主要介绍了详解Spring中bean生命周期回调方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
介绍了Spring的Bean周期,容器周期,工厂周期,运行代码后可以看到运行结果
主要给大家介绍了Spring中Bean的生命周期和作用域及实现方式的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解。这篇文章主要介绍了Spring Bean 生命周期,需要的朋友可以参考下
四份资料介绍Spring Bean 的生命周期,从认识,熟悉,深究,记忆Spring Bean。希望这份资料能帮助你!
NULL 博文链接:https://cici9898-007.iteye.com/blog/1407695
一般情况下,我们只是关心如何正确地将Bean装配到容器中,并不关心Ioc容器是如何装配和销毁Bean的过程。但是恰恰有时候,我们需要自定义初始化或销毁Bean的过程,以满足一些“特殊的”需求。比如,数据源在关闭的...
今天小编就为大家分享一篇关于spring中bean的生命周期详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
Spring的生命周期是指实例化Bean时所经历的一系列阶段,即通过getBean()获取bean对象及设置对象属性时,Spring框架做了哪些事。Bean的生命周期从Spring容器实例化Bean到销毁Bean。 本文分别对 BeanFactory 和 ...
Spring Bean 生命周期之“我从哪里来?”
1、<bean>标签主要用来进行Bean定义; 2、alias用于定义Bean别名的; 3、import用于导入其他配置文件的Bean定义,这是为了加载多个配置文件,当然也可以把这些配置文件构造为一个数组(new String[] {“config1.xml...
Spring管理的Bean的生命周期
学习Spring过程中,使用Eclipse调试Spring源码的关键断点文件。