美文网首页Java
Java Spring-Bean的生命周期

Java Spring-Bean的生命周期

作者: 一亩三分甜 | 来源:发表于2020-01-10 18:37 被阅读0次

    Spring IOC容器对Bean的生命周期进行管理的过程:

    通过构造器或工厂方法创建Bean实例

    为Bean的属性设置值和对其他Bean的引用

    调用Bean的初始化方法

    Bean可以使用了

    当容器关闭时,调用Bean的销毁方法

    在Bean的声明里设置init-method和destroy-method属性,为Bean指定初始化和销毁方法。

        <bean id="car" class="com.cloud.spring.beans.cycle.Car"
        init-method="init2"
        destroy-method="destory"
        >
            <property name="brand" value="Audi"></property>
        </bean>
        
        public class Car {
    
        private String brand;
    
        public Car() {
            System.out.println("Car's Constructor...");
        }
    
        public String getBrand() {
            return brand;
        }
    
        public void setBrand(String brand) {
            System.out.println("setBrand...");
            this.brand = brand;
        }
    
        public void init2(){
            System.out.println("init...");
        }
    
        public void destory(){
            System.out.println("destroy...");
        }
    
        @Override
        public String toString() {
            return "Car{" +
                    "brand='" + brand + '\'' +
                    '}';
        }
    }
    //输出
    Car's Constructor...
    setBrand...
    init...
    com.cloud.spring.beans.cycle.Car@77caeb3e
    destroy...
    

    Bean后置处理器允许在调用初始化方法前后对Bean进行额外的处理。Bean后置处理器对IOC容器里的所有Bean实例逐一处理,而非单一实例,其典型应用是:检查Bean属性的正确性或根据特定的标准更改Bean的属性。对Bean后置处理器而言,需要实现Interface BeanPostProcessor接口,在初始化方法被调用前后,Spring将把每个Bean实例分别传递给上述接口的以下两个方法:

    Object postProcessAfterInitialization(Object bean,String beanName);
    Object postProcessBeforeInitialization(Object bean,String beanName);
    

    例子

        <bean id="car" class="com.cloud.spring.beans.cycle.Car"
        init-method="init2"
        destroy-method="destory"
        >
            <property name="brand" value="Audi"></property>
        </bean>
    
        <!-- 配置bean的后置处理器 -->
        <bean class="com.cloud.spring.beans.cycle.MyBeanPostProcessor"></bean>
        
        public class MyBeanPostProcessor implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException{
            System.out.println("postProcessBeforeInitialization: " + bean + "," + beanName);
            return bean;
        }
    
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException{
            System.out.println("postProcessAfterInitialization: " + bean + "," + beanName );
            return bean;
        }
    }
    
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans-cycle.xml");
            Car car = (Car)context.getBean("car");
            System.out.println(car);
    
            //关闭IOC容器
            context.close();
    //输出
    Car's Constructor...
    setBrand...
    postProcessBeforeInitialization: Car{brand='Audi'},car
    init...
    postProcessAfterInitialization: Car{brand='Audi'},car
    Car{brand='Audi'}
    destroy...
    

    在postProcessAfterInitialization方法中处理返回Bean,最后改变生成的bean。

    public class MyBeanPostProcessor implements BeanPostProcessor {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException{
            System.out.println("postProcessBeforeInitialization: " + bean + "," + beanName);
            return bean;
        }
    
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException{
            System.out.println("postProcessAfterInitialization: " + bean + "," + beanName );
            Car car = new Car();
            car.setBrand("Ford");
            return car;
        }
    }
    //输出
    Car's Constructor...
    setBrand...
    postProcessBeforeInitialization: Car{brand='Audi'},car
    init...
    postProcessAfterInitialization: Car{brand='Audi'},car
    Car's Constructor...
    setBrand...
    Car{brand='Ford'}
    destroy...
    
        <bean id="car" class="com.cloud.spring.beans.cycle.Car"
        init-method="init2"
        destroy-method="destory"
        >
            <property name="brand" value="Audi"></property>
        </bean>
    <!-- 实现BeanPostProcessor接口,并具体提供
         Object postProcessBeforeInitialization(Object bean,String beanName): init-method 之前被调用
         Object postProcessAfterInitialization(Object bean,String beanName):init-method 之后被调用
         的实现
    
         bean:bean 实例本身
         beanName:IOC 容器配置的bean的名字
         返回值:是实际上放回给用户的那个Bean,注意:可以在以上两个方法中修改返回的bean,甚至返回一个新的bean
    -->
        <!-- 配置bean的后置处理器 -->
        <bean class="com.cloud.spring.beans.cycle.MyBeanPostProcessor"></bean>
    

    添加Bean后置处理器后Bean的生命周期

    Spring IOC容器对Bean的生命周期进行管理的过程:

    通过构造器或工厂方法创建Bean实例

    为Bean的属性设置值和对其他Bean的引用

    将Bean实例传递给Bean后置处理器的postProcessBeforeInitialization方法

    调用Bean的初始化方法

    将Bean实例传递给Bean后置处理器的postProcessAfterInitialization方法

    Bean可以使用了

    当容器关闭时,调用Bean的销毁方法

    相关文章

      网友评论

        本文标题:Java Spring-Bean的生命周期

        本文链接:https://www.haomeiwen.com/subject/rajmactx.html