zoukankan      html  css  js  c++  java
  • spring bean中构造函数,afterPropertiesSet和init-method的执行顺序

    http://blog.csdn.net/super_ccc/article/details/50728529

    1.xml文件

    [html] view plain copy
     
     print?
    1. <bean id="aaa" class="com.dingwang.Test.Aaa" init-method="init">  
    2.         <constructor-arg name="name" value="ddd"></constructor-arg>  
    3.     </bean>  


    2.java文件

    [java] view plain copy
     
     print?
    1. public Aaa(String name) {  
    2.        LOGGER.warn("--------------------Aaa-----------------Aaa");  
    3.        this.setName(name);  
    4.    }  
    5.   
    6.    public void init() {  
    7.        LOGGER.warn("--------------------Aaa-----------------init");  
    8.    }  
    9.   
    10.    /* 
    11.     * (non-Javadoc) 
    12.     * @see 
    13.     * org.springframework.beans.factory.InitializingBean#afterPropertiesSet() 
    14.     */  
    15.    @Override  
    16.    public void afterPropertiesSet() throws Exception {  
    17.        LOGGER.warn("--------------------Aaa-----------------afterPropertiesSet");  
    18.    }  


    3.执行日志

    [plain] view plain copy
     
     print?
    1. 10:44:54.116 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'aaa'  
    2. 10:44:54.157 [main] WARN  com.dingwang.Test.Aaa - --------------------Aaa-----------------Aaa  
    3. 10:44:54.159 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'aaa' to allow for resolving potential circular references  
    4. 10:44:54.171 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'aaa'  
    5. 10:44:54.172 [main] WARN  com.dingwang.Test.Aaa - --------------------Aaa-----------------afterPropertiesSet  
    6. 10:44:54.172 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking init method  'init' on bean with name 'aaa'  
    7. 10:44:54.172 [main] WARN  com.dingwang.Test.Aaa - --------------------Aaa-----------------init  
    8. 10:44:54.173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'aaa'  


    4.结论

    执行顺序:构造函数>afterPropertiesSet>init-method

    Person类

    [java] view plain copy
     
     print?
    1. public class Person {  
    2.     private int i = 0;  
    3.   
    4.     public Person(){  
    5.         System.out.println("实例化一个对象");  
    6.     }  
    7.       
    8.     public void init(){  
    9.         System.out.println("调用初始化方法....");  
    10.     }  
    11.       
    12.     public void destory222(){  
    13.         System.out.println("调用销毁化方法....");  
    14.     }  
    15. }  


    applicationContext.xml

    [html] view plain copy
     
     print?
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.         xsi:schemaLocation="http://www.springframework.org/schema/beans   
    5.                             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
    6.     <!-- 配置初始化方法和销毁方法,但是如果要销毁方法生效scope="singleton" -->                  
    7.     <bean id="person" class="com.xxc.initAndDestory.domain.Person" scope="singleton" lazy-init="false" init-method="init" destroy-method="destory"></bean>  
    8. </beans>  


    测试类:

    [java] view plain copy
     
     print?
    1. public class Test {  
    2.     public static void main(String[] args) {  
    3.         //如果要调用销毁方法必须用子类来声明,而不是ApplicationContext,因为ApplicationContext没有close()  
    4.         ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initAndDestory/applicationContext.xml");  
    5.         Person p1 = (Person)ac.getBean("person");  
    6.         Person p2 = (Person)ac.getBean("person");  
    7.         ac.close();  
    8.     }  
    9. }  

    如果用注解方式配置:

    applicationContext.xml

    [html] view plain copy
     
     print?
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:context="http://www.springframework.org/schema/context"   
    4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.        xsi:schemaLocation="http://www.springframework.org/schema/beans     
    6.                             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
    7.                             http://www.springframework.org/schema/context     
    8.                             http://www.springframework.org/schema/context/spring-context-2.5.xsd">    
    9.                               
    10.     <context:annotation-config/>   
    11.                               
    12.     <!-- scope默认是 prototype:getBean()一次创建一个实例-->  
    13.     <bean id="person" class="com.xxc.initAndDestory.domain.Person"></bean>  
    14. </beans>  


    Person类

    [java] view plain copy
     
     print?
      1. public class Person {  
      2.     private int i = 0;  
      3.   
      4.     public Person(){  
      5.         System.out.println("实例化一个对象");  
      6.     }  
      7.     @PostConstruct //初始化方法的注解方式  等同与init-method=init  
      8.     public void init(){  
      9.         System.out.println("调用初始化方法....");  
      10.     }  
      11.     @PreDestroy //销毁方法的注解方式  等同于destory-method=destory222  
      12.     public void destory(){  
      13.         System.out.println("调用销毁化方法....");  
      14.     }  
      15. }  
  • 相关阅读:
    Java进行AES/DES加密,C#对其进行解密
    【程序员的吃鸡大法】利用OCR文字识别+百度算法搜索,玩转冲顶大会、百万英雄、芝士超人等答题赢奖金游戏
    TWaver初学实战——炫动2D机房之连线篇
    TWaver初学实战——炫动2D机房之设备篇
    TWaver初学实战——炫动2D机房之机柜篇
    TWaver初学实战——基于HTML5的交互式地铁图
    TWaver3D入门探索——3D拓扑图之人在江湖
    TWaver3D入门探索——3D拓扑图之绽放的小球花
    TWaver初学实战——如何在TWaver属性表中添加日历控件?
    TWaver初学实战——如何在EasyUI中插入TWaver(续)
  • 原文地址:https://www.cnblogs.com/exmyth/p/7236839.html
Copyright © 2011-2022 走看看