ApplicationContext中Bean的生命周期

ApplicationContext中Bean的生命周期

Car bean实例类

package com.woshi.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

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

    private BeanFactory beanFactory;
    private String beanName;

    public Car() {
        System.out.println("调用Car的无参构造");
    }

    public void setBrand(String brand) {
        System.out.println("调用brand的属性的setter方法");
        this.brand = brand;
    }

    public void setColor(String color) {
        System.out.println("调用color属性的setter方法");
        this.color = color;
    }

    public void setMaxSpeed(int maxSpeed) {
        System.out.println("调用maxSpeed属性的setter方法");
        this.maxSpeed = maxSpeed;
    }


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

    public void setBeanName(String name) {
        System.out.println("调用BeanNameAware.setBeanName()!");
        this.beanName = name;
    }


    public void afterPropertiesSet() throws Exception {
        System.out.println("调用InitializingBean.afterPropertiesSet()!");
    }

    public void destroy() throws Exception {
        System.out.println("调用DisposableBean.destroy()!");
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("调用ApplicationContextAware.setApplicationContext()!");
    }

    public void myInit(){
        System.out.println("调用init-method指定的myInit(),将maxSpeed设置为210");
        this.maxSpeed = 210;
    }

    public void myDestory(){
        System.out.println("调用destory-method指定的myDestory()!");
    }


    public String getBrand() {
        return brand;
    }

    public String getColor() {
        return color;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public String getBeanName() {
        return beanName;
    }

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

}

MyBeanPostProcessor.java

package com.woshi.context;

import com.woshi.pojo.Car;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("car")) {
            Car car = (Car) bean;
            if (car.getColor() == null) {
                System.out.println("调用bean后处理器的postProcessBeforeInitialization(),color为空,设置color为black");
                car.setColor("black");
            }
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("car")) {
            Car car = (Car) bean;
            if (car.getMaxSpeed() >= 200) {
                System.out.println("调用bean后处理器的postProcessAfterInitialization()方法,发现maxSpeed大于200,将其调整为200");
                car.setMaxSpeed(200);
            }
        }
        return bean;
    }
}

MyBeanFactoryPostProcessor.java

package com.woshi.context;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    //对car<bean>的brand属性配置信息进行“偷梁换柱”的加工操作
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        BeanDefinition beanDefinition = beanFactory.getBeanDefinition("car");
        
        beanDefinition.getPropertyValues().addPropertyValue("brand", "保时捷");
        System.out.println("调用BeanFactoryPostProcessor.postProcessBeanFactory()!");
    }
}

MyInstantiationAwareBeanPostProcessor.java

package com.woshi.context;

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 ("car".equals(beanName)) {
            System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation()!");
        }
        return null;
    }

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

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if ("car".equals(beanName)) {
            System.out.println("InstantiationAwareBeanPostProcessor.postProcessProperties()!");
        }
        return pvs;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if ("car".equals(beanName)) {
            System.out.println("InstantiationAwareBeanPostProcessor.postProcessBeforeInitialization()!");
        }
        return null;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("car".equals(beanName)) {
            System.out.println("InstantiationAwareBeanPostProcessor.postProcessAfterInitialization()!");
        }
        return null;
    }
}

test.java

import com.woshi.pojo.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Car car = (Car) applicationContext.getBean("car");
        System.out.println(car);
        ((ConfigurableApplicationContext)applicationContext).registerShutdownHook();
    }
}

beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="car" class="com.woshi.pojo.Car" init-method="myInit" destroy-method="myDestory"/>

<!--    工厂后处理器-->
    <bean id="myBeanFactoryPostProcessor" class="com.woshi.context.MyBeanFactoryPostProcessor"/>
<!--    注册Bean后处理器-->
    <bean id="myBeanPostProcessor" class="com.woshi.context.MyBeanPostProcessor"/>
    <bean id="myInstantiationAwareBeanPostProcessor" class="com.woshi.context.MyInstantiationAwareBeanPostProcessor"/>
</beans>

输出结果:
ApplicationContext中Bean的生命周期

ApplicationContext中Bean的生命周期

上一篇:角色移动优化【Unity2D自学之路】


下一篇:Java毕业设计+现成产品:企业人事管理系统(java+SSM+jsp+mysql+maven)