bean生命周期

来源:互联网 发布:古筝教学软件 编辑:程序博客网 时间:2024/06/07 05:11





注意:1.spring容器中可以注册多个后置处理器,它们的顺序可以通过实现org.springframework.core.Ordered接口来决定调用顺序。
            2.ApplicationContext 和BeanFactory的bean生命周期不同:
                      2.1:applicationContext中如果bean实现了org.springframework.beans.BeansException.ApplicationContextAware接口,则会调用该接口的setApplicationContext()方法;
                      2.2:后置处理器,例如:BeanPostProcessor,BeanFactoryPostProcessor,InstantiationAwareBeanPostProcessor,不需要手动注册到容器,容器会根据配置文件,自动注册到容器中。



举个例子:

package com.yaspeed.spring.bean.cycle;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 Iphone7 implements BeanNameAware,BeanFactoryAware,InitializingBean,DisposableBean{private String name;private int price;private BeanFactory beanFactory;private String beanName;public Iphone7(){System.out.println("调用构造函数Iphone7()...");}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}@Overridepublic void destroy() throws Exception {// TODO Auto-generated method stubSystem.out.println("调用DisposableBean.destroy()...");}@Overridepublic void afterPropertiesSet() throws Exception {// TODO Auto-generated method stubSystem.out.println("调用InitializingBean.afterPropertiesSet()...");}@Overridepublic void setBeanFactory(BeanFactory bf) throws BeansException {// TODO Auto-generated method stubthis.beanFactory=bf;System.out.println("调用BeanFactoryAware.setBeanFactory()...");}@Overridepublic void setBeanName(String beanName) {// TODO Auto-generated method stubthis.beanName=beanName;System.out.println("调用BeanNameAware.setBeanName()...");}public void init(){System.out.println("调用Init-method:myInitMethod()...");this.name="iphone7";}public void cleanUp(){System.out.println("调用destory-method:myDestoryMethod()...");}}


<?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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="iphone7" class="com.yaspeed.spring.bean.cycle.Iphone7" scope="singleton" init-method="init"destroy-method="cleanUp" p:name="iphone7plus" p:price="7188"></bean><!-- 测试applicationcontext生命周期,spring根据下面配置自动注册后置处理器 --><bean id="myBeanPostProcessor" class="com.yaspeed.spring.bean.cycle.MyBeanPostProcessor"></bean><bean id="myInstantiationAwareBeanPostProcessor" class="com.yaspeed.spring.bean.cycle.MyInstantiationAwareBeanPostProcessor"></bean></beans>


package com.yaspeed.spring.bean.cycle;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPostProcessor implements BeanPostProcessor{@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {// TODO Auto-generated method stubif("iphone7".equals(beanName)){System.out.println("MyBeanPostProcessor.postProcessAfterInitialization()...");}return bean;}@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {// TODO Auto-generated method stubif("iphone7".equals(beanName)){System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization()...");}return bean;}}


package com.yaspeed.spring.bean.cycle;import java.beans.PropertyDescriptor;import org.springframework.beans.BeansException;import org.springframework.beans.PropertyValues;import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {@Overridepublic Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {if("iphone7".equals(beanName)){System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation()...");}return null;}@Overridepublic boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {if("iphone7".equals(beanName)){System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation()...");}return true;}@Overridepublic PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)throws BeansException {if("iphone7".equals(beanName)){System.out.println("InstantiationAwareBeanPostProcessor.postProcessPropertyValues()...");}return pvs;}}


package com.yaspeed.spring.bean.cycle;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[] args) {                //测试ApplicationContext中bean生命周期        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("bean-cycle.xml");        Iphone7 iphone7 =(Iphone7)ctx.getBean("iphone7");        System.out.println(iphone7);        ctx.close();        //测试BeanFactory中bean生命周期        /*Resource resource = new ClassPathResource("bean-cycle.xml");        XmlBeanFactory bf = new XmlBeanFactory(resource);        bf.addBeanPostProcessor(new MyBeanPostProcessor());        bf.addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());        Iphone7 iphone7 =(Iphone7)bf.getBean("iphone7");        bf.destroySingletons();*/            }}

运行结果:
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation()...
调用构造函数Iphone7()...
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation()...
InstantiationAwareBeanPostProcessor.postProcessPropertyValues()...
调用BeanNameAware.setBeanName()...
调用BeanFactoryAware.setBeanFactory()...
MyBeanPostProcessor.postProcessBeforeInitialization()...
调用InitializingBean.afterPropertiesSet()...
调用Init-method:myInitMethod()...
MyBeanPostProcessor.postProcessAfterInitialization()...
com.yaspeed.spring.bean.cycle.Iphone7@49993335
九月 13, 2016 10:09:47 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7106e68e: startup date [Tue Sep 13 10:09:47 CST 2016]; root of context hierarchy
调用DisposableBean.destroy()...
调用destory-method:myDestoryMethod()...









0 0
原创粉丝点击