zoukankan      html  css  js  c++  java
  • SpringIOC容器中Bean的生命周期

    SpringIOC容器对Bean的生命周期的管理过程:

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

        2、为Bean的属性赋值或对其他Bean的引用

        3、调用Bean的初始化方法

        4、使用Bean

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

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

    package cycle;
    
    /**
     * @author chenpeng
     * @date 2018/6/2 11:53
     */
    public class Car {
        public Car() {
            System.out.println("Contructor...");
        }
    
        private String brand;
    
        public void setBrand(String brand) {
            System.out.println("setBrand...");
            this.brand = brand;
        }
    
        public void init(){
            System.out.println("init....");
        }
    
        public void destroy(){
            System.out.println("destroy... ");
        }
    }
    
    <?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.xsd">
    
        <!--利用init-method、destroy-method调用Bean中的初始和销毁方法-->
        <bean id="car" class="cycle.Car" init-method="init" destroy-method="destroy"
              p:brand="大众"/>
    </beans>
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean_cycle.xml");
        Car car = (Car) context.getBean("car");
        System.out.println(car);
    
        //关闭容器
        context.close();
    }


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

    - Bean的后置处理器允许在调用初始化方法前后对Bean进行额外的处理

    - Bean的后置处器对IOC容器的所有Bean是逐一处理,而非单一实例。

    - Bean的后置处理器需要实现BeanPostProcessor接口。

    使用后置处理器SpringIOC容器对Bean的生命周期进行管理:

    1、使用构造器或工厂方法创建Bean实例

    2、为Bean的属性设置值或对其他Bean的引用

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

    4、调用Bean的初始化方法

    5、将Bean的实例传递给Bean的后置处理器的PostProcessAfterInitialization方法

    6、使用Bean

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

    package cycle;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    /**
     * @author chenpeng
     * @date 2018/6/2 13:09
     */
    public class MyBeanPostProcessor implements BeanPostProcessor {
        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("postProcessBeforeInitialization:"+bean+","+beanName);
            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.xsd">
    
        <!--利用init-method、destroy-method调用Bean中的初始和销毁方法-->
        <bean id="car" class="cycle.Car" init-method="init" destroy-method="destroy"
              p:brand="大众"/>
    
        <!--配置后置处理器-->
        <bean class="cycle.MyBeanPostProcessor"/>
    
    </beans>

    可以在后置处理器中对Bean的属性值进行更改

    改前:

    后:

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization:"+bean+","+beanName);
        Car car = new Car();
        car.setBrand("宝马");
        return car;
    }


    使用后置处理器:

    <!--
    使用后置处理器需要实现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
    -->
    <!--不需要配置id,IOC容器自动识别是一个BeanPostProcessor-->
    <bean class="cycle.MyBeanPostProcessor"/>


  • 相关阅读:
    【POJ】【2104】区间第K大
    【BZOJ】【2595】【WC2008】游览计划
    【BZOJ】【1036】树的统计
    【BZOJ】【2038】小Z的袜子
    我的博客~
    CSS选择器
    CSS样式表分类
    python奇闻杂技03 三元表达式示例,函数定义示例,七段彩码管绘制示例
    python奇闻杂技02 time模块,文本进度条示例,数字类型操作,字符串操作
    python奇闻杂技01 turtle学习
  • 原文地址:https://www.cnblogs.com/huangzhe1515023110/p/9276060.html
Copyright © 2011-2022 走看看