spring bean中构造函数,afterPropertiesSet和init-method的执行顺序

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

1.xml文件

  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文件

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

3.执行日志

  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类

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

applicationContext.xml

  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>

测试类:

  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

  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. <context:annotation-config/>
  10. <!-- scope默认是 prototype:getBean()一次创建一个实例-->
  11. <bean id="person" class="com.xxc.initAndDestory.domain.Person"></bean>
  12. </beans>

Person类

    1. public class Person {
    2. private int i = 0;
    3. public Person(){
    4. System.out.println("实例化一个对象");
    5. }
    6. @PostConstruct //初始化方法的注解方式  等同与init-method=init
    7. public void init(){
    8. System.out.println("调用初始化方法....");
    9. }
    10. @PreDestroy //销毁方法的注解方式  等同于destory-method=destory222
    11. public void destory(){
    12. System.out.println("调用销毁化方法....");
    13. }
    14. }
上一篇:搭建spring+mybatis+struts2环境的配置文件


下一篇:tomcat集群待整理