zoukankan      html  css  js  c++  java
  • Spring学习记录(八)---Bean的生命周期

    之前说过,在调用下面时,就创建了容器和对象

    1    ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");

    那它又是怎样一步步创建的呢?要销毁怎么销毁?

    用一个例子来看

     1 package com.guigu.spring.Car;
     2 
     3 public class Car {
     4     private String brand;
     5     
     6     public Car(){
     7         System.out.println("构造函数。。");
     8     }
     9     public String getBrand() {
    10         System.out.println("返回属性。。");
    11         return brand;
    12     }
    13     public void setBrand(String brand) {
    14         this.brand = brand;
    15         System.out.println("设置属性。。");
    16     }
    17     public void init(){
    18         System.out.println("init()。。");
    19     }
    20     public void destroy(){
    21         System.out.println("destroy()。。");
    22     }
    23 }

     xml中用init-mehod、 destroy-method表示调用初始化函数和销毁函数

    1 <bean id="car" class="com.guigu.spring.Car" init-method="init" destroy-method="destroy">
    2 <property name="brand" value="Aodi"></property>
    3 </bean>

    main中

    1   ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
    2   Car car=(Car)ctx.getBean("car");
    3   System.out.println(car);
    4    //关闭IOC容器
    5   ctx.close();

    输出:

      构造函数。。
      设置属性。。
      init()。。
      car[brand=Aodi]
      destroy()。。

    可以看出来,先调用构造函数和设置属性,然后再init()。

     bean后置处理器:检查bean属性的正确性或根据需要修改属性,要实现BeanPostProcessor接口

    写一个实现类MyBeanPostProcessor

     1 package com.guigu.spring.Car;
     2 
     3 import org.springframework.beans.BeansException;
     4 import org.springframework.beans.factory.config.BeanPostProcessor;
     5 
     6 public class MyBeanPostProcessor implements BeanPostProcessor {
     7 
     8     @Override    //before
     9     public Object postProcessBeforeInitialization(Object bean, String beanName)
    10             throws BeansException {
    11         System.out.println("postProcessBeforeInitialization: "+bean +beanName);
    12         return bean;
    13     }
    14 
    15     @Override    //after
    16     public Object postProcessAfterInitialization(Object bean, String beanName)
    17             throws BeansException {
    18         System.out.println("postProcessAfterInitialization: "+bean +beanName);
    19         return bean;
    20     }
    21 }

    xml中添加此接口

    1   <bean id="car" class="com.guigu.spring.Car" init-method="init" destroy-method="destroy">
    2   <property name="brand" value="Aodi"></property>
    3   </bean>
    4     <!--配置bean后置处理器,不需要配置id-->
    5   <bean class="com.guigu.spring.Car"></bean>

    再重新运行一次,输出:

       构造函数。。
       设置属性。。
       postProcessBoforeInitialization: car[brand=Aodi], car 
    init()。。 postProcessAfterInitialization: car[brand=Aodi], car car[brand=Aodi] destroy()。。

    注意:一个再init之前调用,一个再init之后调用 

    如何修改属性? 在接口函数中修改

     1 package com.guigu.spring.Car;
     2 
     3 import org.springframework.beans.BeansException;
     4 import org.springframework.beans.factory.config.BeanPostProcessor;
     5 
     6 import com.guigu.spring.autowireautowire.Car;
     7 
     8 public class MyBeanPostProcessor implements BeanPostProcessor {
     9 
    10     @Override
    11     public Object postProcessBeforeInitialization(Object bean, String beanName)
    12             throws BeansException {
    13         System.out.println("postProcessBeforeInitialization: "+bean +beanName);
    14         return bean;
    15     }
    16 
    17     @Override
    18     public Object postProcessAfterInitialization(Object bean, String beanName)
    19             throws BeansException {
    20         System.out.println("postProcessAfterInitialization: "+bean +beanName);
    21         Car car = (Car) bean;   // 获取要修改的bean对象
    22         car.setBrand("Ford");   //修改属性
    23         return bean;
    24     }
    25 }

    注意:上面的bean是bean实例本身;beanName指IOC容器配置的bean的名字

    这样,就成功修改了属性,输出: 

    1    构造函数。。
    2    设置属性。。
    3    postProcessBoforeInitialization:car[brand=Aodi], car 
    4    init()。。
    5    postProcessAfterInitialization: car[brand=Aodi], car
    6    
    7    car[brand=Ford]
    8    destroy()。。

     还可以过滤,因为处理所有bean时,可能对某些进行操作

     1     @Override
     2     public Object postProcessBeforeInitialization(Object bean, String beanName)
     3             throws BeansException {
     4       System.out.println("postProcessBeforeInitialization: "+bean +beanName);
     5 
     6          if("car".equals(beanName)){
     7             //过滤的操作
     8     }
     9        return bean;
    10     }

    再看一遍bean生命周期

  • 相关阅读:
    SQLSERVER中的sp_reset_connection存储过程的作用
    SQLSERVER数据库经常置疑的原因
    sqlserver2005数据库邮件
    SQLSERVER书签查找的通俗理解
    msdb数据库里的表究竟存储什么信息
    造成阻塞和死锁的3大原因:
    SQLSERVER中的锁资源类型RID KEY PAG EXT TAB DB FIL
    总结一下要影响SQLSERVER锁的申请和释放行为要考虑的因素
    Linux下getsockopt/setsockopt 函数说明
    HTTP协议详解(转)
  • 原文地址:https://www.cnblogs.com/ooooevan/p/5804269.html
Copyright © 2011-2022 走看看