bean生命周期





注意: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;
	}
	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("调用DisposableBean.destroy()...");
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("调用InitializingBean.afterPropertiesSet()...");
	}
	@Override
	public void setBeanFactory(BeanFactory bf) throws BeansException {
		// TODO Auto-generated method stub
		this.beanFactory=bf;
		System.out.println("调用BeanFactoryAware.setBeanFactory()...");
		
	}
	@Override
	public void setBeanName(String beanName) {
		// TODO Auto-generated method stub
		this.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{

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		if("iphone7".equals(beanName)){
			System.out.println("MyBeanPostProcessor.postProcessAfterInitialization()...");
		}
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		// TODO Auto-generated method stub
		if("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 {
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		
		if("iphone7".equals(beanName)){
			System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation()...");
		}
		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		if("iphone7".equals(beanName)){
			System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation()...");
		}
		return true;
	}

	@Override
	public 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()...









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值