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....

  • 相关阅读:
    C++中重载,重写,隐藏的区别
    以太网(局域网)交换机工作原理
    IP地址、MAC地址、ARP地址解析协议
    Metasploitable渗透测试实战——Windows漏洞 MS08-067复现
    一次对真实网站的SQL注入———SQLmap使用
    多字节与宽字节转换
    char*、string、CString各种字符串之间转换
    国密SM4分组加密算法实现 (C++)
    网络编程——基于UDP的网络化CPU性能检测
    U盘小偷——C++实现U盘插入检测和文件扫描拷贝
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5682656.html
Copyright © 2011-2022 走看看