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

  • 相关阅读:
    java作用域public ,private ,protected 及不写时的区别
    Android开发环境部署
    java的WebService实践(cxf)
    Servlet小试
    XML内容作为String字符串读取报错
    myeclipse安装svn插件的多种方式
    一个简单的SpringMVC3 程序
    VS2010调试生成的文件
    双系统启动项修复
    安装服务命令
  • 原文地址:https://www.cnblogs.com/solverpeng/p/5682656.html
Copyright © 2011-2022 走看看