initializingbean 是在如何给bean定义作用域之前吗

博客分类:
在Bean的声明周期中,有两个事件尤为重要:post-initialization和pre-destruction。
Spring提供了两种机制:interface-based和method-based,供bean签入上述事件。
所谓的post-initialization和pre-destruction,是指在bean的属性设置完毕执行的事件和在bean销毁之前执行的事件。
method-based机制:通过在BeanFactory中的配置,init-method和destory-method,通过设置这两个属性来指定要执行的回调方法。
interface-based机制:bean需要实现InitializatingBean接口和DisposableBean接口。这两个接口分别拥有afterPropertiesSet()方法和destroy()方法。这两个方法在bean初始化后和销毁前被执行。效果与method-based机制等同。
以下是网上流传一个例子,我在eclipse中运行了一下
package com.
import javax.annotation.PostC
import javax.annotation.PreD
import org.springframework.beans.BeansE
import org.springframework.beans.factory.BeanF
import org.springframework.beans.factory.BeanFactoryA
import org.springframework.beans.factory.BeanNameA
import org.springframework.beans.factory.DisposableB
import org.springframework.beans.factory.InitializingB
public class LifeCycle implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean {
public LifeCycle() {
System.out.println("Constructor executing");
public void setCycle(String cycle) {
System.out.println("Setter methods executing");
this.cycle =
public void setBeanName(String beanName) {
System.out.println("BeanNameAware.setBeanName(String beanName) executing");
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)");
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)");
public void destroy() throws Exception {
System.out.println("DisposableBean.destroy() executing");
public void destroyMethod() {
System.out.println("DestroyMethod executing");
&?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.test.LifeCycle" init-method="initMethod" destroy-method="destroyMethod"&
&property name="cycle" value="Life Cycle"/&
&bean id="beanPostProcessor" class="org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor"&
&property name="initAnnotationType" value="javax.annotation.PostConstruct"/&
&property name="destroyAnnotationType" value="javax.annotation.PreDestroy"/&
package com.
import org.springframework.beans.factory.config.BeanPostP
import org.springframework.beans.factory.xml.XmlBeanF
import org.springframework.core.io.ClassPathR
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");
最后运行结果是:
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
注入Bean具体的实现:具体的bean创建过程和依赖关系的注入在createBean中,这个方法在AbstractAutowireCapableBeanFactory中给出了实现:
protected Object createBean(String beanName, RootBeanDefinition
mergedBeanDefinition, Object[] args)
throws BeanCreationException {
// Guarantee initialization of beans that the current one depends on.
// 这里对取得当前bean的所有依赖bean,确定能够取得这些已经被确定的bean,如果没有被创建,那么这个createBean会被这些IOC
// getbean时创建这些bean
if (mergedBeanDefinition.getDependsOn() != null) {
for (int i = 0; i & mergedBeanDefinition.getDependsOn(). i++) {
getBean(mergedBeanDefinition.getDependsOn()[i]);
// 这里是实例化bean对象的地方,注意这个BeanWrapper类,是对bean操作的主要封装类
if (instanceWrapper == null) {
instanceWrapper = createBeanInstance(beanName, mergedBeanDefinition,args);
Object bean = instanceWrapper.getWrappedInstance();
//这个populate方法,是对已经创建的bean实例进行依赖注入的地方,会使用到在loadBeanDefinition的时候得到的那些propertyValue来对bean进行注入。
if (continueWithPropertyPopulation) {
populateBean(beanName, mergedBeanDefinition, instanceWrapper);
//这里完成客户自定义的对bean的一些初始化动作
Object originalBean =
bean = initializeBean(beanName, bean, mergedBeanDefinition);
// Register bean as disposable, and also as dependent on specified "dependsOn"beans.
registerDisposableBeanIfNecessary(beanName, originalBean,mergedBeanDefinition);
浏览: 47005 次
来自: 杭州
基于微博数据检测的Solr5.5实战开发课程观看地址:http ...
期待后面的项目实例:接收带有附件的邮件 发送html形式的邮件 ...在spring容器初始化bean和销毁bean的以前的操作有很多种,  目前我知道的有:在xml中定义的时候用init-method和destory-method,还有一种就是定义bean的时候实现DisposableBean和InitializingBean 这两个接口,打开InitializingBean 的源码:
public interface InitializingBean {
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* &p&This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
void afterPropertiesSet() throws E
根据注解很清楚的可以看出,afterPropertiesSet()表示在资源加载完以后,初始化bean之前执行的方法,我猜想spring底层应该会在初始化bean的时候,应该会使用(bean instanceof&InitializingBean)判断是不是实现了这个接口,其实在很多框架中都是这么干的,但是因为没研究过spring源码,暂且还不知道底层原理。这样我们就可以在初始化的时候,做一些自己想要做的事了。  同理,DisposableBean就是在一个bean被销毁的时候,spring容器会帮你自动执行这个方法,估计底层原理也是差不多的,对于一些使用完之后需要释放资源的bean,我们都会实现这个接口,或者是配置destory-method方法。源码也基本是相似的,只是把afterPropertiesSet改为destroy。
ApplicationContextAware  其实我们看到---Aware就知道是干嘛用的了,就是属性注入的,但是这个ApplicationContextAware的不同地方在于,实现了这个接口的bean,当spring容器初始化的时候,会自动的将ApplicationContext注入进来:
import mons.lang.V
import org.springframework.beans.BeansE
import org.springframework.beans.factory.DisposableB
import org.springframework.context.ApplicationC
import org.springframework.context.ApplicationContextA
import org.springframework.context.annotation.L
import org.springframework.stereotype.S
* applicationContext静态化
* 使用了ApplicationContextAware接口的类,如果受spring容器管理的
* 话,那么就会自动的调用ApplicationContextAware中的setApplicationContext方法
* @author Hotusm
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware,DisposableBean{
private static ApplicationContext applicationC
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringContextHolder.applicationContext=applicationC
//清空applicationContext 设置其为null
public void destroy() throws Exception {
SpringContextHolder.clearHolder();
//获得applicationContext
public static ApplicationContext getApplicationContext() {
//assertContextInjected();
return applicationC
public static void clearHolder(){
applicationContext=null;
//获取Bean
public static &T& T getBean(Class&T& requiredType){
//assertContextInjected();
return (T) getApplicationContext().getBean(requiredType);
@SuppressWarnings("unchecked")
public static &T& T getBean(String name){
assertContextInjected();
return (T) getApplicationContext().getBean(name);
//判断application是否为空
public static void assertContextInjected(){
Validate.isTrue(applicationContext==null, "application未注入 ,请在springContext.xml中注入SpringHolder!");
因为我们在做开发的时候,并不是说在每一个地方都能将属性注入到我们想要的地方去的,比如在Utils使用到dao,我们就不能直接注入了,这个时候就是我们需要封装springContext的时候了,而ApplicationContextAware就起了关键性的作用。
3:还有一种是注解的用法:
在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。
阅读(...) 评论()博客分类:
装配的组件模型,一切实体类都可以配置成一个
,进而就可以在任何其他的
中使用,一个
也可以不是指定的实体类,这就是抽象
中有两个最基本、最重要的包,即
org.springframework.beans
org.springframework.context
,在这两个包中,为了实现无侵入式的框架,代码中大量地使用了
中的反射机制,通过动态调用来避免硬编码,为
反向控制提供了基础保证,在这两个包中,最重要的类时
BeanFactory
ApplicationContext
BeanFactory
提供一种先进的配置机制来管理任何种类的
ApplicationContext
BeanFactory
之上的,并增加了其他功能,例如,国际化的支持、资源的访问和事件传播等。
属性具有唯一性,每一个
只能有一个对应的
属性可以指定一个或者多个名称,各个名称之间用逗号或者分号隔开,第一个默认为标识名称,后面的多个自动成为这个
配置文件中
的来源,也就是
的实际路径,它指向一个实体类。
中可以直接在配置文件中指定类的作用域,
的作用域。
只有两种作用域,即
Singleton(
non-Singleton(
prototype)
以后,增加了
global session
三个专用于
应用程序上下文的
的作用域设置为
Spring IOC
容器中只存在一个共享的
实例,并且所有对
的请求,只要
定义相匹配,则只会返回
的同一个实例。这个单一实例会被存储到单例缓存
(singleton cache)
中,并且所有针对该
的后续请求和引用都将返回被缓存的对象实例。
的作用域部署的
,每一个请求都会产生一个新的
实例,相当于一个
的操作,对于
,有一点非常重要,那就是
不能对一个
的整个生命周期复杂,容器在初始化、配置、装饰或者是装配完一个
实例后,将它交给客户端,随后就对该
实例不负责了。
表示针对每一个
请求都会产生一个新的
HTTP Request
作用域表示针对在一个
请求都会产生一个新的
Http session
范围内有效。
global session
global session
作用域类似于标准的
HTTP Session
的生命周期
从建立到销毁,会历经几个执行阶段,如果使用
BeanFactory
来生成、管理
,会尽量支持一下的生命周期。
BeanFactory
的定义文件,并生成各个
执行相关的
属性依赖注入
BeanNameAware
setBeanName()
org.springframework.beans.factory.BeanNameAware
接口,则执行它的
setBeanName()
BeanFactoryAware
setBeanFactory()
org.springframework.beans.factory.BeanFactoryAware
接口,则执行它的
setBeanFactory()
BeanPostProcessors
processBeforeInitialization()
如果有任何的
org.springframework.beans.factory.config.BeanPostProcessor
实例相关联,则执行
BeanPostProcessor
processBeforeInitialization()
InitializingBean
afterPropertiesSet()
org.springframework.beans.factory.InitializingBean
,则执行它的
afterPropertiesSet()
定义文件中定义
init-method
定义文件使用
”init-method”
属性设置方法名称。
BeanPostProcessors
processAfterInitialization()
如果有任何的
org.springframework.beans.factory.config.BeanPostProcessor
实例相关联,则执行
BeanPostProcessor
processAfterInitialization()
DisposableBean
在容器关闭时,如果
org.springframework.beans.factory.DisposableBean
接口,则执行它的
定义文件中定义
destroy –method
在容器关闭时,可以在
定义文件使用
destroy –method
属性设置方法名称。
FactoryBean
中有两种类型的
,一种是普通的
可以是用户定义的任何类;另一种是工厂
FactoryBean
不同,其返回的对象不是指定类的一个实例,其返回的是该工厂
方法返回的对象。在
框架内部,
相关的功能及事务处理中,很多方法使用到工厂
BeanPostProcessor
的依赖关系由
容器建立并设置以后,你还有机会定义一些
的修正动作来修正相关的属性,方法是让
org.springframework.beans.factory.config.BeanPostProcessor
接口,该接口与
中的定义如下:
public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansE
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansE
postProcessBeforeInitialization
类被初始化之前
InitializingBean
afterPropertiesSet()
方法或者自定义的
被执行,而
postProcessAfterInitialization()
类被初始化之后立即被执行。
浏览: 297516 次
来自: 杭州
浏览量:10424
表露清晰, 言简意赅, 重新温习了一次
新版本都不需要了。ServiceLoader&Drive ...
像以上的实验,就没有出现数据幻读的问题。
---原因:MyS ...
贴代码来了。大家还要根据代码做一定的修改。########## ...
ThreadPoolService类中的queue、threa ...博客分类:
可参考文章:
容器启动时:
BeanFactoryPostProcessor
-&postProcessBeanFactory()
getBean时:
实例化之后调用:
InstantiationAwareBeanPostProcessor
-&postProcessPropertyValues()
初始化时:
属性注入(setter)
BeanNameAware
-&setBeanName()
BeanFactoryAware
-&setBeanFactory()
ApplicationContextAware
-&setApplicationContext()
BeanPostProcessor
-&postProcessBeforeInitialization()
InitializingBean
-&afterPropertiesSet()
init-method属性
BeanPostProcessor
-&postProcessAfterInitialization()
DiposibleBean
-&destory()
destroy-method属性
BeanFactoryPostProcessor
//Spring IoC容器允许BeanFactoryPostProcessor在容器实际实例化任何其它的bean之前读取配置元数据,并有可能修改它。
//同时BeanFactoryPostProcessor的回调比BeanPostProcessor要早
void postProcessBeanFactory(ConfigurableListableBeanFactory arg)
InstantiationAwareBeanPostProcessorAdapter
其适配器类:InstantiationAwareBeanPostProcessor
//实例化Bean之前调用 (Bean构造函数前)
Object postProcessBeforeInstantiation(Class&?& beanClass, String beanName)
//实例化Bean之后调用
boolean postProcessAfterInstantiation(Object bean, String beanName)
PropertyValues postProcessPropertyValues(PropertyValues pvs,
PropertyDescriptor[] pds, Object bean, String beanName)
BeanPostProcessor
//实例化、依赖注入完毕,在初始化之前调用,完成一些定制的初始化任务
Object postProcessBeforeInitialization(Object bean, String beanName)
//实例化、依赖注入、初始化完毕时执行
Object postProcessAfterInitialization(Object bean, String beanName)
BeanFactoryPostProcessor和BeanPostProcessor都是Spring初始化bean的扩展点。两个接口非常相似。
BeanFactoryPostProcessor可以在postProcessBeanFactory()中对bean的定义(配置元数据)进行处理,而BeanPostProcessor不可以。
同时BeanFactoryPostProcessor的回调比BeanPostProcessor要早。
百度文库:
实例2(重要):
Bean的完整生命周期经历了各种方法调用,这些方法可以划分为以下几类:
1、Bean自身的方法:
  这个包括了Bean本身调用的方法和通过配置文件中&bean&的init-method和destroy-method指定的方法
2、Bean级生命周期接口方法:
  这个包括了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这些接口的方法
3、容器级生命周期接口方法:
  这个包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。
4、工厂后处理器接口方法:
  这个包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工厂后处理器  接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。
我们用一个简单的Spring Bean来演示一下Spring Bean的生命周期。
1、首先是一个简单的Spring Bean,调用Bean自身的方法和Bean级生命周期接口方法,
为了方便演示,它实现了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这4个接口,
【对于BeanFactoryAware和BeanNameAware接口,第一个接口让bean感知容器(即BeanFactory实例,从而以此获取该容器配置的其他bean对象),而后者让bean获得配置文件中对应的配置名称。在一般情况下用户不需要关心这两个接口。如果bean希望获得容器中的其他bean,可以通过属性注入的方式引用这些bean。如果bean希望在运行期获知在配置文件中的Bean名称,可以简单的将名称作为属性注入
综上所述,我们认为除非编写一个基于spring之上的扩展框架插件或者子项目之类的东西,否则用户完全可以抛开以上4个bean生命周期的接口类
但BeanPostProcessor接口却不一样,它不要求bean去继承它,它可以完全像插件一样注册到spring容器中,为容器提供额外的功能。spring充分利用了BeanPostProcessor对bean进行加工处理(SpringAOP以此为基础)】
同时有2个方法,对应配置文件中&bean&的init-method和destroy-method。如下:
package springBeanT
import org.springframework.beans.BeansE
import org.springframework.beans.factory.BeanF
import org.springframework.beans.factory.BeanFactoryA
import org.springframework.beans.factory.BeanNameA
import org.springframework.beans.factory.DisposableB
import org.springframework.beans.factory.InitializingB
public class Person implements BeanFactoryAware, BeanNameAware,
InitializingBean, DisposableBean {
private BeanFactory beanF
private String beanN
public Person() {
System.out.println("【构造器】调用Person的构造器实例化");
public String getName() {
public void setName(String name) {
System.out.println("【注入属性】注入属性name");
this.name =
public String getAddress() {
public void setAddress(String address) {
System.out.println("【注入属性】注入属性address");
this.address =
public int getPhone() {
public void setPhone(int phone) {
System.out.println("【注入属性】注入属性phone");
this.phone =
public String toString() {
return "Person [address=" + address + ", name=" + name + ", phone="
+ phone + "]";
// 这是BeanFactoryAware接口方法
public void setBeanFactory(BeanFactory arg) throws BeansException {
System.out
.println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
this.beanFactory =
// 这是BeanNameAware接口方法
public void setBeanName(String arg) {
System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
this.beanName =
// 这是InitializingBean接口方法
public void afterPropertiesSet() throws Exception {
System.out
.println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
// 这是DiposibleBean接口方法
public void destroy() throws Exception {
System.out.println("【DiposibleBean接口】调用DiposibleBean.destory()");
// 通过&bean&的init-method属性指定的初始化方法
public void myInit() {
System.out.println("【init-method】调用&bean&的init-method属性指定的初始化方法");
// 通过&bean&的destroy-method属性指定的初始化方法
public void myDestory() {
System.out.println("【destroy-method】调用&bean&的destroy-method属性指定的初始化方法");
2、接下来是演示BeanPostProcessor接口的方法,如下:
package springBeanT
import org.springframework.beans.BeansE
import org.springframework.beans.factory.config.BeanPostP
public class MyBeanPostProcessor implements BeanPostProcessor {
public MyBeanPostProcessor() {
System.out.println("这是BeanPostProcessor实现类构造器!!");
public Object postProcessAfterInitialization(Object arg, String arg)
throws BeansException {
System.out.println("BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!");
public Object postProcessBeforeInitialization(Object arg, String arg)
throws BeansException {
System.out.println("BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!");
如上,BeanPostProcessor接口包括2个方法postProcessAfterInitialization和postProcessBeforeInitialization,这两个方法的第一个参数都是要处理的Bean对象,第二个参数都是Bean的name。返回值也都是要处理的Bean对象。这里要注意。
3、InstantiationAwareBeanPostProcessor 接口本质是BeanPostProcessor的子接口,一般我们继承Spring为其提供的适配器类InstantiationAwareBeanPostProcessorAdapter来使用它,如下:
package springBeanT
import java.beans.PropertyD
import org.springframework.beans.BeansE
import org.springframework.beans.PropertyV
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorA
public class MyInstantiationAwareBeanPostProcessor extends
InstantiationAwareBeanPostProcessorAdapter {
public MyInstantiationAwareBeanPostProcessor() {
System.out.println("这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!!");
// 接口方法、实例化Bean之前调用
public Object postProcessBeforeInstantiation(Class beanClass,
String beanName) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法");
// 接口方法、实例化Bean之后调用
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法");
// 接口方法、设置某个属性时调用
public PropertyValues postProcessPropertyValues(PropertyValues pvs,
PropertyDescriptor[] pds, Object bean, String beanName)
throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法");
这个有3个方法,其中第二个方法postProcessAfterInitialization就是重写了BeanPostProcessor的方法。第三个方法postProcessPropertyValues用来操作属性,返回值也应该是PropertyValues对象。
4、演示工厂后处理器接口方法,如下:
package springBeanT
import org.springframework.beans.BeansE
import org.springframework.beans.factory.config.BeanD
import org.springframework.beans.factory.config.BeanFactoryPostP
import org.springframework.beans.factory.config.ConfigurableListableBeanF
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public MyBeanFactoryPostProcessor() {
System.out.println("这是BeanFactoryPostProcessor实现类构造器!!");
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg)
throws BeansException {
System.out
.println("BeanFactoryPostProcessor调用postProcessBeanFactory方法");
BeanDefinition bd = arg.getBeanDefinition("person");
bd.getPropertyValues().addPropertyValue("phone", "");
5、配置文件如下beans.xml,很简单,使用ApplicationContext,处理器不用手动注册:
&?xml version="1.0" encoding="UTF-8"?&
&beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"&
&bean id="beanPostProcessor" class="springBeanTest.MyBeanPostProcessor"&&/bean&
&bean id="instantiationAwareBeanPostProcessor" class="springBeanTest.MyInstantiationAwareBeanPostProcessor"&&/bean&
&bean id="beanFactoryPostProcessor" class="springBeanTest.MyBeanFactoryPostProcessor"&
&bean id="person" class="springBeanTest.Person" init-method="myInit"
destroy-method="myDestory" scope="singleton" p:name="张三" p:address="广州"
p:phone="" /&
package springBeanT
import org.springframework.context.ApplicationC
import org.springframework.context.support.ClassPathXmlApplicationC
public class BeanLifeCycle {
public static void main(String[] args) {
System.out.println("现在开始初始化容器");
ApplicationContext ctx = new ClassPathXmlApplicationContext("springBeanTest/beans.xml");
System.out.println("容器初始化成功");
//得到Preson,并使用
Person person = ctx.getBean("person",Person.class);
System.out.println(person);
System.out.println("现在开始关闭容器!");
((ClassPathXmlApplicationContext)ctx).registerShutdownHook();
结果如下:
现在开始初始化容器
15:46:20 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@19a0c7c: startup date [Sun May 18 15:46:20 CST 2014]; root of context hierarchy
15:46:20 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [springBeanTest/beans.xml]
这是BeanFactoryPostProcessor实现类构造器!
BeanFactoryPostProcessor调用postProcessBeanFactory方法
这是BeanPostProcessor实现类构造器!!
这是InstantiationAwareBeanPostProcessorAdapter实现类构造器!!
15:46:20 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9934d4: defining beans [beanPostProcessor,instantiationAwareBeanPostProcessor,beanFactoryPostProcessor,person]; root of factory hierarchy
InstantiationAwareBeanPostProcessor调用postProcessBeforeInstantiation方法
【构造器】调用Person的构造器实例化
InstantiationAwareBeanPostProcessor调用postProcessPropertyValues方法
【注入属性】注入属性address
【注入属性】注入属性name
【注入属性】注入属性phone
【BeanNameAware接口】调用BeanNameAware.setBeanName()
【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()
BeanPostProcessor接口方法postProcessBeforeInitialization对属性进行更改!
【InitializingBean接口】调用InitializingBean.afterPropertiesSet()
【init-method】调用&bean&的init-method属性指定的初始化方法
BeanPostProcessor接口方法postProcessAfterInitialization对属性进行更改!
InstantiationAwareBeanPostProcessor调用postProcessAfterInitialization方法(这个位置感觉有问题,待解决)
容器初始化成功
Person [address=广州, name=张三, phone=110]
现在开始关闭容器!
【DiposibleBean接口】调用DiposibleBean.destory()
【destroy-method】调用&bean&的destroy-method属性指定的初始化方法
图示如下:
浏览 32782
浏览: 3406390 次
来自: 一片神奇的土地
写的很好!
可以看看bboss session,支持集群session共享 ...
实现一个智能提示功能需要JavaScript、ajax、数据库 ...
这个例子是 的 211 985 高校客户案 ...}

我要回帖

更多关于 bean定义 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信