Spring 中 Bean 的生命周期

Spring 中 Bean 的生命周期

参考资料:http://blog.csdn.net/a327369238/article/details/52193822

 BeanPostProcessor   http://www.cnblogs.com/sishang/p/6576665.html

 

 

BeanNameAware  让Bean获取自己在BeanFactory配置中的名字 

BeanFactoryAware  让Bean获取配置他们的BeanFactory的引用

BeanNameAware  与 BeanFactoryAware 参考

InitializingBean   它只包括afterPropertiesSet方法 spring初始化bean的时候,如果bean实现了InitializingBean接口,会自动调用afterPropertiesSet方法  参考

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 BeanLifeCyleTest implements BeanNameAware,BeanFactoryAware,InitializingBean,DisposableBean {
    
    private String message;
    
    private BeanFactory beanFactory;

    public BeanLifeCyleTest() {
        System.out.println("构造器方法......");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("destroy方法......");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用 afterPropertiesSet 方法");
    }

    @Override
    public void setBeanFactory(BeanFactory beanfactory) throws BeansException {
        this.beanFactory = beanfactory;
        System.out.println("setBeanFactory 方法......" + beanFactory);
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("setBeanName 方法......beanName:" + name);
    }
    
    public void beanInit(){
        System.out.println("init-method 方法调用");
    }

    public void beanDestroy(){
        System.out.println("destroy-method 方法调用");
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
}

 BeanFactoryPostProcessor    http://www.cnblogs.com/sishang/p/6588542.html

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

/**
 * Spring IoC容器允许BeanFactoryPostProcessor在容器实例化任何bean之前读取bean的定义(配置元数据),并可以修改它。
 * 同时可以定义多个BeanFactoryPostProcessor,通过设置'order'属性来确定各个BeanFactoryPostProcessor执行顺序
 * 
 * 
 */
public class BeanFactoryPostProcessorTest implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("BeanFactoryPostProcessorTest postProcessBeanFactory 被调用");
    }

}
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * 在Spring容器完成bean实例化、配置以及其他初始化方法前后要添加一些自己逻辑处理。我们需要定义一个或多个BeanPostProcessor接口实现类,然后注册到Spring IoC容器中。
 */
public class InitBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
        System.out.println("BeanPostProcessor postProcessAfterInitialization bean = " + arg0.getClass());
        return arg0;
    }

    @Override
    public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
        System.out.println("BeanPostProcessor postProcessBeforeInitialization bean = " + arg0.getClass());
        return arg0;
    }
}

import java.beans.PropertyDescriptor;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

/**
* InstantiationAwareBeanPostProcessor是BeanPostProcessor的子接口,可以在Bean生命周期的另外两个时期提供扩展的回调接口,
* 即实例化Bean之前(调用postProcessBeforeInstantiation方法)和实例化Bean之后(调用postProcessAfterInstantiation方法)
*
*/
public class InstanceBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

@Override
public Object postProcessAfterInitialization(Object obj, String arg1) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor postProcessAfterInitialization 被调用 ");
return obj;
}

@Override
public Object postProcessBeforeInitialization(Object obj, String arg1) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor postProcessBeforeInitialization 被调用");
return obj;
}

@Override
public boolean postProcessAfterInstantiation(Object obj, String arg1) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor postProcessAfterInstantiation 返回boolean,bean实例化后调用,并且返回false则不会注入属性");
return true;
}

@Override
public Object postProcessBeforeInstantiation(Class classz, String arg1) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor postProcessBeforeInstantiation 被调用 " + classz.getName());
return null;
}

@Override
public PropertyValues postProcessPropertyValues(PropertyValues propertyValues, PropertyDescriptor[] PropertyDescriptors, Object obj,
String str) throws BeansException {
System.out.println("InstantiationAwareBeanPostProcessor postProcessPropertyValues 被调用");
return propertyValues;
}

}

XML配置

"1.0" encoding="UTF-8"?>
"http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
        
        base-package="springBeanLifeCycle" />
        
        "beanLifeCyleTest" class="springBeanLifeCycle.BeanLifeCyleTest" init-method="beanInit" destroy-method="beanDestroy">
           "beanName" value="test" />
        
        
        class="springBeanLifeCycle.InstanceBeanPostProcessor"/>
        class="springBeanLifeCycle.InitBeanPostProcessor"/>
        class="springBeanLifeCycle.BeanFactoryPostProcessorTest"/>
        

测试类

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("springBeanLifeCycle.xml");
        BeanLifeCyleTest beanLifeCyleTest = context.getBean("beanLifeCyleTest", BeanLifeCyleTest.class);
        System.out.println("bean name :" + beanLifeCyleTest.getMessage());
((ClassPathXmlApplicationContext) context).close(); } }

测试结果

BeanFactoryPostProcessorTest postProcessBeanFactory 被调用
InstantiationAwareBeanPostProcessor postProcessBeforeInstantiation 被调用 org.springframework.context.event.EventListenerMethodProcessor
InstantiationAwareBeanPostProcessor postProcessAfterInstantiation 返回boolean,bean实例化后调用,并且返回false则不会注入属性
InstantiationAwareBeanPostProcessor postProcessPropertyValues 被调用
InstantiationAwareBeanPostProcessor postProcessBeforeInitialization 被调用
BeanPostProcessor postProcessBeforeInitialization bean = class org.springframework.context.event.EventListenerMethodProcessor
InstantiationAwareBeanPostProcessor postProcessAfterInitialization 被调用
BeanPostProcessor postProcessAfterInitialization bean = class org.springframework.context.event.EventListenerMethodProcessor
InstantiationAwareBeanPostProcessor postProcessBeforeInstantiation 被调用 org.springframework.context.event.DefaultEventListenerFactory
InstantiationAwareBeanPostProcessor postProcessAfterInstantiation 返回boolean,bean实例化后调用,并且返回false则不会注入属性
InstantiationAwareBeanPostProcessor postProcessPropertyValues 被调用
InstantiationAwareBeanPostProcessor postProcessBeforeInitialization 被调用
BeanPostProcessor postProcessBeforeInitialization bean = class org.springframework.context.event.DefaultEventListenerFactory
InstantiationAwareBeanPostProcessor postProcessAfterInitialization 被调用
BeanPostProcessor postProcessAfterInitialization bean = class org.springframework.context.event.DefaultEventListenerFactory
InstantiationAwareBeanPostProcessor postProcessBeforeInstantiation 被调用 springBeanLifeCycle.BeanLifeCyleTest
构造器方法......
InstantiationAwareBeanPostProcessor postProcessAfterInstantiation 返回boolean,bean实例化后调用,并且返回false则不会注入属性
InstantiationAwareBeanPostProcessor postProcessPropertyValues 被调用
setBeanName 方法......beanName:test
setBeanName 方法......beanName:beanLifeCyleTest
setBeanFactory 方法......org.springframework.beans.factory.support.DefaultListableBeanFactory@55d56113: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,beanLifeCyleTest,springBeanLifeCycle.InstanceBeanPostProcessor#0,springBeanLifeCycle.InitBeanPostProcessor#0,springBeanLifeCycle.BeanFactoryPostProcessorTest#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy
InstantiationAwareBeanPostProcessor postProcessBeforeInitialization 被调用
BeanPostProcessor postProcessBeforeInitialization bean = class springBeanLifeCycle.BeanLifeCyleTest
调用 afterPropertiesSet 方法
init-method 方法调用
InstantiationAwareBeanPostProcessor postProcessAfterInitialization 被调用
BeanPostProcessor postProcessAfterInitialization bean = class springBeanLifeCycle.BeanLifeCyleTest
bean name :null
三月 28, 2018 9:50:49 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@31cefde0: startup date [Wed Mar 28 21:50:49 CST 2018]; root of context hierarchy
destroy方法......
destroy-method 方法调用

 

由结果可以看出调用顺序为

1.  调用   BeanFactoryPostProcessor   实现类的    postProcessBeanFactory  方法

2.  调用  InstantiationAwareBeanPostProcessor   实现类的   postProcessBeforeInstantiation   方法
3.  类本身的构造器方法
4.  调用  InstantiationAwareBeanPostProcessor  实现类的   postProcessAfterInstantiation  方法
5.  调用  InstantiationAwareBeanPostProcessor  实现类的   postProcessPropertyValues   方法
6.  调用  BeanNameAware  接口的   setBeanName  方法
7.  调用  BeanFactoryAware  接口的   setBeanFactory   方法
8.  调用  InstantiationAwareBeanPostProcessor  实现类的  postProcessBeforeInitialization  方法
9.  调用  BeanPostProcessor  接口实现类的  postProcessBeforeInitialization  方法
10.调用   xml bean 中配置的  init-method 方法
11.调用  InstantiationAwareBeanPostProcessor  接口实现类的  postProcessAfterInitialization   方法
12.调用  BeanPostProcessor  接口实现类的  postProcessAfterInitialization   方法
13.调用  DisposableBean  接口的  destroy   方法
14.调用  xml bean 中配置的  destroy-method 方法

posted on 2018-03-28 22:28  code前行 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/codefeng/p/8570439.html

你可能感兴趣的:(Spring 中 Bean 的生命周期)