欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

BeanFactory中Bean的生命周期

程序员文章站 2022-05-22 20:02:40
...
/**
 * Car.java
 * cn.com.songjy.test.spring
 * Function: TODO 
 *
 *   version    date      author
 * ──────────────────────────────────
 *   	1.0	 2013-9-6    songjy
 *
 * Copyright (c) 2013, TNT All Rights Reserved.
 */

package cn.com.songjy.test.spring;

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;

/**
 * ClassName:Car
 * 
 * @author songjy
 * @version 1.0
 * @since v1.0
 * @Date 2013-9-6 上午9:52:19
 */

public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean,
		DisposableBean {

	public Car() {
		System.out.println("调用Car()构造函数");
	}

	private String brand;
	private String color;
	private int maxSpeed;

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public int getMaxSpeed() {
		return maxSpeed;
	}

	public void setMaxSpeed(int maxSpeed) {
		this.maxSpeed = maxSpeed;
	}

	public String getBrand() {
		return brand;
	}
	
	public void setBrand(String brand) {
		System.out.println("调用setBrand()设置属性");
		this.brand = brand;
	}

	public void introduce() {
		System.out.println("brand:" + brand + ";color:" + color + ";maxSpeed:"
				+ maxSpeed);
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.DisposableBean#destroy()
	 */
	@Override
	public void destroy() throws Exception {
		System.out.println("Car.destroy()");
	}

	/* 通过<bean>的destroy-method属性指定的销毁方法 */
	public void myDestroy() {
		System.out.println("调用destroy-method所指定的myDestroy()");
	}

	/* 通过<bean>的init-method属性指定的初始化方法 */
	public void myInit() {
		System.out.println("调用init-method所指定的myInit(),将maxSpeed设置为240。");
		this.maxSpeed = 240;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Car.afterPropertiesSet()");
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)
	 */
	@Override
	public void setBeanName(String arg0) {
		System.out.println("Car.setBeanName()");
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
	 */
	@Override
	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		System.out.println("Car.setBeanFactory()");
	}

}

/**
 * MyInstantiationAwareBeanPostProcessor.java
 * cn.com.songjy.test.spring
 * Function: TODO 
 *
 *   version    date      author
 * ──────────────────────────────────
 *   	1.0	 2013-9-6    songjy
 *
 * Copyright (c) 2013, TNT All Rights Reserved.
*/

package cn.com.songjy.test.spring;

import java.beans.PropertyDescriptor;

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

/**
 * ClassName:MyInstantiationAwareBeanPostProcessor
 *
 * @author   songjy
 * @version  1.0
 * @since    v1.0
 * @Date	 2013-9-6	上午10:05:18
 */

public class MyInstantiationAwareBeanPostProcessor extends
		InstantiationAwareBeanPostProcessorAdapter {

	/**
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessBeforeInstantiation(java.lang.Class, java.lang.String)
	 */
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass,
			String beanName) throws BeansException {/*接口方法:在实例化Bean前进行调用*/
		
		if("car".equals(beanName)) {/*仅对容器中car Bean进行处理*/
			System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
		}
		
		return super.postProcessBeforeInstantiation(beanClass, beanName);/*等价于 return null;*/
		
	}

	/**
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessAfterInstantiation(java.lang.Object, java.lang.String)
	 */
	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName)
			throws BeansException {/*接口方法:在实例化Bean后调用*/
		
		if("car".equals(beanName)) {/*仅对容器中car Bean进行处理*/
			System.out.println("InstantiationAware BeanPostProcessor. postProcessBeforeInstantiation");
		}
		
		return super.postProcessAfterInstantiation(bean, beanName);/*等价于return true;*/
		
	}

	/**
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessPropertyValues(org.springframework.beans.PropertyValues, java.beans.PropertyDescriptor[], java.lang.Object, java.lang.String)
	 */
	@Override
	public PropertyValues postProcessPropertyValues(PropertyValues pvs,
			PropertyDescriptor[] pds, Object bean, String beanName)
			throws BeansException {/*接口方法:在设置某个属性时调用*/
		
		if("car".equals(beanName)) {/*仅对容器中car Bean进行处理,还可以通过pdst入参进行过滤,仅对car的某个特定属性时进行处理*/
			System.out.println("InstantiationAware BeanPostProcessor.postProcessPropertyValues");
		}
		
		return super.postProcessPropertyValues(pvs, pds, bean, beanName);/*等价于  return pvs;*/
		
	}

	
	
}
/**
 * MyBeanPostProcessor.java
 * cn.com.songjy.test.spring
 * Function: TODO 
 *
 *   version    date      author
 * ──────────────────────────────────
 *   	1.0	 2013-9-6    songjy
 *
 * Copyright (c) 2013, TNT All Rights Reserved.
*/

package cn.com.songjy.test.spring;

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

/**
 * ClassName:MyBeanPostProcessor
 *
 * @author   songjy
 * @version  1.0
 * @since    v1.0
 * @Date	 2013-9-6	上午10:26:58
 */

public class MyBeanPostProcessor implements BeanPostProcessor {

	/**
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String)
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {

		if("car".equals(beanName)) {
			Car car = (Car) bean;
			if(null ==car.getColor()) {
				System.out
						.println("调用BeanPostProcessor.postProcessBeforeInitialization(),olor为空,设置为默认黑色");
				car.setColor("黑色");
			}
		}
		
		return bean;

	}

	/**
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String)
	 */
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {

		if("car".equals(beanName)) {
			Car car = (Car) bean;
			if(car.getMaxSpeed() >= 200) {
				System.out
						.println("调用BeanPostProcessor.postProcessAfterInitialization(),将maxSpeed调整为200");
				car.setMaxSpeed(200);
			}
		}
		
		return bean;

	}

}

 

<?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="cn.com.songjy.test.spring.Car" init-method="myInit"
		destroy-method="myDestroy" p:brand="红旗CA72" p:maxSpeed="200"></bean>

</beans>

 

/**
 * BeanLifeCycle.java
 * cn.com.songjy.test.spring
 * Function: TODO 
 *
 *   version    date      author
 * ──────────────────────────────────
 *   	1.0	 2013-9-6    songjy
 *
 * Copyright (c) 2013, TNT All Rights Reserved.
 */

package cn.com.songjy.test.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;/*XmlBeanFactory已过时*/
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * ClassName:BeanLifeCycle
 * 
 * @author songjy
 * @version 1.0
 * @since v1.0
 * @Date 2013-9-6 上午10:38:27
 */

public class BeanLifeCycle {

	public static void main(String[] args) {

		Resource res = new ClassPathResource("cn/com/songjy/test/spring/beans.xml");
		BeanFactory bf = new XmlBeanFactory(res);
		
		//向容器中注册MyBeanPostProcessor后处理器
		((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyBeanPostProcessor());
		
		//向容器中注册MyInstantiationAwareBeanPostProcessor后处理器
		((ConfigurableBeanFactory)bf).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
		
		//第一次从容器中获取car,将触发容器实例化该Bean,这将引发Bean生命周期方法的调用
		Car car1 = (Car)bf.getBean("car");
		car1.introduce();
		car1.setColor("红色");
		
		//第二次从容器中获取car,直接从缓存池中获取
		Car car2 = (Car)bf.getBean("car");
		
		//查看car1和car2是否指向同一引用
		System.out.println("car1==car2:"+(car1==car2));
		
		//关闭容器
		((XmlBeanFactory)bf).destroySingletons();
	}
}

 

【Spring】IOC核心源码学习(二):容器初始化过程 http://singleant.iteye.com/blog/1177358

转载于:https://my.oschina.net/sjyroom/blog/159572