• 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. }  
  • 相关阅读:
    [poj3974] Palindrome 解题报告 (hashmanacher)
    SQL Android
    SharedPreferences Android
    本地广播 localBroadcastManager Android
    RecyclerView Android
    自定义ListView Android
    布局 Android
    传值 Android
    活动的生命周期 Android
    CodeForces 907F Power Tower(扩展欧拉定理)
  • 原文地址:https://www.cnblogs.com/exmyth/p/7236839.html
Copyright © 2020-2023  润新知