zoukankan      html  css  js  c++  java
  • Spring重点—— IOC 容器中 Bean 的生命周期

    一、理解 Bean 的生命周期,对学习 Spring 的整个运行流程有极大的帮助。

    二、在 IOC 容器中,Bean 的生命周期由 Spring IOC 容器进行管理。

    三、在没有添加后置处理器的情况下 Bean 的生命周期

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

    2.为 Bean 的属性设置值好对其他 Bean 的引用

    3.调用 Bean 的初始化方法

    4.Bean 可以使用了

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

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

    例如:

    /**
     * @author solverpeng
     * @create 2016-07-18-20:42
     */
    public class Life {
        private String lifeName;
    
        public void setLifeName(String lifeName) {
            System.out.println("setLifeName....");
            this.lifeName = lifeName;
        }
    
        public Life() {
            System.out.println("constructor....");
        }
    
        public void initMethod() {
            System.out.println("initMethod....");
        }
    
        public void destroyMethod() {
            System.out.println("destroyMethod....");
        }
    
        public void targetMethod() {
            System.out.println("targetMethod....");
        }
    
    
    }

    spring-config.xml

    <bean class="com.nucsoft.spring.bean.Life" id="life" init-method="initMethod" destroy-method="destroyMethod">
      <property name="lifeName" value="myLife"/>
    </bean>

    Test

    @Test
    public void test03() {
      Life life = ctx.getBean(Life.class);
      life.targetMethod();
    }

    控制台输出:

    constructor....
    setLifeName....
    initMethod....
    targetMethod....

    四、Bean 后置处理器

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

    2.Bean 后置处理器对 IOC 容器里的所有 Bean 实例逐一处理。

    3.具体使用:需要实现 BeanPostProcessor 接口,实现 postProcessBeforeInitialization(Object bean, String beanName) 和 postProcessAfterInitialization(Object bean, String beanName) 两个方法。

    分别在 初始化方法前后被调用。

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

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

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

    3.将 Bean 实例传递给 bean 后置处理器的 postProcessBeforeInitialization() 方法

    4.调用 Bean 的初始化方法

    5.将 Bean 实例传递给 bean 后置处理器的 postProcessAfterInitialization() 方法。

    6.使用 Bean

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

    例如:

    /**
     * @author solverpeng
     * @create 2016-07-18-20:42
     */
    public class Life {
        private String lifeName;
    
        public void setLifeName(String lifeName) {
            System.out.println("setLifeName....");
            this.lifeName = lifeName;
        }
    
        public Life() {
            System.out.println("constructor....");
        }
    
        public void initMethod() {
            System.out.println("initMethod....");
        }
    
        public void destroyMethod() {
            System.out.println("destroyMethod....");
        }
    
        public void targetMethod() {
            System.out.println("targetMethod....");
        }
    
    
    }
    Life.java
    /**
     * @author solverpeng
     * @create 2016-07-18-20:58
     */
    public class MyBeanPostProcessor implements BeanPostProcessor{
        
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            if(bean instanceof Life) {
                System.out.println("life's postProcessBeforeInitialization....");
            }
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if(bean instanceof Life) {
                System.out.println("life's postProcessAfterInitialization....");
            }
            return bean;
        }
    }
    MyBeanPostProcessor.java
    <bean class="com.nucsoft.spring.processor.MyBeanPostProcessor"/>
    
    <bean class="com.nucsoft.spring.bean.Life" id="life" init-method="initMethod" destroy-method="destroyMethod">
      <property name="lifeName" value="myLife"/>
    </bean>

    Test

    @Test
    public void test03() {
      Life life = ctx.getBean(Life.class);
      life.targetMethod();
    }

    控制台输出:

    constructor....
    setLifeName....
    life's postProcessBeforeInitialization....
    initMethod....
    life's postProcessAfterInitialization....
    targetMethod....

  • 相关阅读:
    URLs
    上班确实累!!!
    转: java 双向map
    HttpReader
    QQ龙虎榜数据接口
    简易行情界面
    下载新浪的行情数据
    淘宝上的大智慧L2数据,月卡最便宜是8元钱,这个也可以获取BBD、DDX等数据!
    获取历史K线数据的几个方法
    好久不写博了.
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5682656.html
Copyright © 2011-2022 走看看