Bean的生命周期

1.domain

package com.baobaotao.domain;

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 Car implements BeanFactoryAware,BeanNameAware,InitializingBean,DisposableBean {
	private String brand;
	private String color;
	private   double price ;  
	private int maxSpeed;
	private BeanFactory beanFactory;
	private String beanName;
	/**
	 * @return the brand
	 */
	public String getBrand() {
		return brand;
	}
	/**
	 * @param brand the brand to set
	 */
	public void setBrand(String brand) {
		System.out.println("调用setBrand()设置属性");
		this.brand = brand;
	}
	/**
	 * @return the color
	 */
	public String getColor() {
		return color;
	}
	/**
	 * @param color the color to set
	 */
	public void setColor(String color) {
		this.color = color;
	}
	/**
	 * @return the maxSpeed
	 */
	public int getMaxSpeed() {
		return maxSpeed;
	}
	/**
	 * @param maxSpeed the maxSpeed to set
	 */
	public void setMaxSpeed(int maxSpeed) {
		this.maxSpeed = maxSpeed;
	}
	
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public Car() {
		System.out.println("调用Car()构造函数");
	}
	public Car(String brand, String color, int maxSpeed) {
		super();
		this.brand = brand;
		this.color = color;
		this.maxSpeed = maxSpeed;
	}
	
	public void myInit() {		
		System.out.println("调用myInit(),将maxSpeed设置为240。");
		this.maxSpeed = 240;
	}
	
	public void myDestory() {
		System.out.println("调用myDestroy()。");
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", color=" + color + ", maxSpeed="
				+ maxSpeed + "]";
	}
	
	public void introduce(){
		System.out.println(this.toString());
	}
	@Override
	public void destroy() throws Exception {
		System.out.println("调用DisposableBean.destroy()");
		
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("调用InitializingBean.afterPropertiesSet()");
		
		
	}
	@Override
	public void setBeanName(String name) {
		System.out.println("调用beanNameAware.setBeanName()");
		this.beanName = beanName;
	}
	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("调用beanFactoryAware.setBeanFactory()");
		this.beanFactory = beanFactory;
	}
}

2.适配器InstantiationAwareBeanPostProcessorAdapter

package com.baobaotao.beanfactory;

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{
	public Object postProcessBeforeInstantiation(Class beanClass, String beanName)throws BeansException{
		if("car".equals(beanName)){
			System.out.println("InstantiationAware BeanPostProcessor.postProcessBeforeInstantiation");
		}
		return null;
	}
	public boolean postProcessAfterInstantiation(Object bean, String beanName)throws BeansException{
		if("car".equals(beanName)){
			System.out.println("InstantiationAware BeanPostProcessor.postProcessAfterInstantiation");
		}
		return true;
	}
	public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)throws BeansException{
		if("car".equals(beanName)){
			System.out.println("Instantiation AwareBeanPostProcessor.postProcessPropertyValues");
		}
		return pvs;
	}
	
}

3.BeanPostProcessor实现

package com.baobaotao.beanfactory;

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

import com.baobaotao.domain.Car;

public class MyBeanPostProcessor implements BeanPostProcessor{

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		if(beanName.equals("car")){
			Car car = (Car)bean;
			if (car.getColor()==null){
				System.out.println("调用BeanPostProcessor.postProcess BeforeInitialization(),color为空,设置为默认黑色");
				car.setColor("黑色");
			}
		}
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		if(beanName.equals("car")){
			Car car = (Car)bean;
			if (car.getMaxSpeed()>=200){
				System.out.println("调用BeanPostProcessor.postProcess AfterInitialization(),将maxSpeed调整为200");
				car.setMaxSpeed(200);;
			}
		}
		return bean;
	}

}


4.beans.xml配置

<?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-3.0.xsd">
	<bean id="car" class="com.baobaotao.domain.Car" init-method="myInit"
		destroy-method="myDestory" p:brand="红旗CA72" p:maxSpeed="200" scope="singleton" />
</beans>

5.容器装载配置文件,注册两个处理器

package com.baobaotao.beanfactory;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.baobaotao.domain.Car;

public class BeanLifeCycle {
	private static void LifeCycleInBeanFactory(){
		Resource res = new ClassPathResource("com/baobaotao/beanfactory/beans.xml");
		BeanFactory bf = new XmlBeanFactory(res);
		
		((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyBeanPostProcessor());
		((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
		Car car1 = (Car)bf.getBean("car");
		car1.introduce();
		car1.setColor("红色");
		
		Car car2 = (Car)bf.getBean("car");
		
		System.out.println("car1==car2:"+(car1==car2));
		((XmlBeanFactory)bf).destroySingletons();
	}
	public static void main(String[] args) {
		LifeCycleInBeanFactory();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值