Spring Bean 声明周期

public class BeanLifeCycle {
    public static void main(String[] args) {
        Resource resource = new ClassPathResource("application-context/spring/beans.xml");
        BeanFactory beanFactory = new XmlBeanFactory(resource);
        ((ConfigurableBeanFactory)beanFactory).addBeanPostProcessor(new MyBeanPostProcessor());
        ((ConfigurableBeanFactory)beanFactory).addBeanPostProcessor(new MyInstantiationAwareBeanPostProcessor());
        ((ConfigurableBeanFactory)beanFactory).addBeanPostProcessor(new InitDestroyAnnotationBeanPostProcessor());

        Car car1 = (Car) beanFactory.getBean("car");
        System.out.println(car1.toString());
        car1.setColor("red");

        System.out.println("****************************************************************");

        Car car2 = (Car) beanFactory.getBean("car");
        System.out.println(car2.toString());
        System.out.println("car1 == car2: " + (car1 == car2));
        ((XmlBeanFactory)beanFactory).destroySingletons();

    }
}

public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if ("car".equals(beanName)) {
            System.out.println("MyInstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation");
        }
        return super.postProcessBeforeInstantiation(beanClass, beanName);
    }

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

    @Override
    public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
        if ("car".equals(beanName)) {
            System.out.println("MyInstantiationAwareBeanPostProcessor.postProcessPropertyValues");
        }
        return super.postProcessPropertyValues(pvs, pds, bean, beanName);
    }
}

public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object object, String beanName) throws BeansException {
        if ("car".equals(beanName)) {
            Car car = (Car) object;
            if (car.getColor() == null) {
                System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization. set Color black");
                car.setColor("black");
            }
        }
        return object;
    }

    @Override
    public Object postProcessAfterInitialization(Object object, String beanName) throws BeansException {
        if ("car".equals(beanName)) {
            Car car = (Car) object;
            if (car.getMaxSpeed() > 200) {
                System.out.println("MyBeanPostProcessor.postProcessAfterInitialization, set MaxSpeed to 200.");
                car.setMaxSpeed(200);
            }
        }
        return object;
    }
}

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        BeanDefinition db = configurableListableBeanFactory.getBeanDefinition("car");
        db.getPropertyValues().addPropertyValue("brand", "qiruiQQ");
        System.out.println("MyBeanFactoryPostProcessor.postProcessBeanFactory");
    }
}


public class Car implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean {
    private String brand;
    private String color;
    private int maxSpeed;

    private BeanFactory beanFactory;
    private String beanName;

    public Car() {
        System.out.println("call constructor");
    }

    public Car(String brand, String color, int maxSpeed) {
        this.brand = brand;
        this.color = color;
        this.maxSpeed = maxSpeed;
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("BeanFactoryAware setBeanFactory");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("BeanNameAware setBeanName");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean destroy");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean afterPropertiesSet");
    }

    public void setBrand(String brand) {
        System.out.println("call setBrand");
        this.brand = brand;
    }

    public void setColor(String color) {
        System.out.println("call setColor");
        this.color = color;
    }

    public void setMaxSpeed(int maxSpeed) {
        System.out.println("call setMaxSpeed");
        this.maxSpeed = maxSpeed;
    }

    public String getBrand() {
        return brand;
    }

    public String getColor() {
        return color;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public void myInit() {
        System.out.println("init-method.");
        this.maxSpeed = 240;
    }

    public void myDestroy() {
        System.out.println("destroy-method.");
    }

    @PostConstruct
    public void postConstruct() {
        System.out.println("postConstruct");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("preDestroy");
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", color='" + color + '\'' +
                ", maxSpeed=" + maxSpeed +
                '}';
    }
}

<?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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:security="http://www.springframework.org/schema/security"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
    http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.6.xsd ">

	<bean id="car" class="org.anicloud.spring4.spring.lifecycle.Car"
			p:brand="bwm"
			p:maxSpeed="200"
			init-method="myInit"
			destroy-method="myDestroy"
			scope="singleton"/>

</beans>

你可能感兴趣的:(spring)