您的位置:首页 > 编程语言 > Java开发

Bean的生命周期

2015-11-30 16:10 691 查看

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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring