http://blog.csdn.net/super_ccc/article/details/50728529
1.xml文件
- <bean id="aaa" class="com.dingwang.Test.Aaa" init-method="init">
- <constructor-arg name="name" value="ddd"></constructor-arg>
- </bean>
2.java文件
- public Aaa(String name) {
- LOGGER.warn("--------------------Aaa-----------------Aaa");
- this.setName(name);
- }
- public void init() {
- LOGGER.warn("--------------------Aaa-----------------init");
- }
- /*
- * (non-Javadoc)
- * @see
- * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
- */
- @Override
- public void afterPropertiesSet() throws Exception {
- LOGGER.warn("--------------------Aaa-----------------afterPropertiesSet");
- }
3.执行日志
- 10:44:54.116 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'aaa'
- 10:44:54.157 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------Aaa
- 10:44:54.159 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'aaa' to allow for resolving potential circular references
- 10:44:54.171 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'aaa'
- 10:44:54.172 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------afterPropertiesSet
- 10:44:54.172 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking init method 'init' on bean with name 'aaa'
- 10:44:54.172 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------init
- 10:44:54.173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'aaa'
4.结论
执行顺序:构造函数>afterPropertiesSet>init-method
Person类
- public class Person {
- private int i = 0;
- public Person(){
- System.out.println("实例化一个对象");
- }
- public void init(){
- System.out.println("调用初始化方法....");
- }
- public void destory222(){
- System.out.println("调用销毁化方法....");
- }
- }
applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <!-- 配置初始化方法和销毁方法,但是如果要销毁方法生效scope="singleton" -->
- <bean id="person" class="com.xxc.initAndDestory.domain.Person" scope="singleton" lazy-init="false" init-method="init" destroy-method="destory"></bean>
- </beans>
测试类:
- public class Test {
- public static void main(String[] args) {
- //如果要调用销毁方法必须用子类来声明,而不是ApplicationContext,因为ApplicationContext没有close()
- ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initAndDestory/applicationContext.xml");
- Person p1 = (Person)ac.getBean("person");
- Person p2 = (Person)ac.getBean("person");
- ac.close();
- }
- }
如果用注解方式配置:
applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config/>
- <!-- scope默认是 prototype:getBean()一次创建一个实例-->
- <bean id="person" class="com.xxc.initAndDestory.domain.Person"></bean>
- </beans>
Person类
- public class Person {
- private int i = 0;
- public Person(){
- System.out.println("实例化一个对象");
- }
- @PostConstruct //初始化方法的注解方式 等同与init-method=init
- public void init(){
- System.out.println("调用初始化方法....");
- }
- @PreDestroy //销毁方法的注解方式 等同于destory-method=destory222
- public void destory(){
- System.out.println("调用销毁化方法....");
- }
- }