初始化两种常见方法:
1.在xml配置bean中写入:init-method="start",start为类中的初始化方法。
2.在类中实例化InitializingBean接口,重写接口的 afterPropertiesSet()方法。
销毁的两种常见方法:
1.在xml配置bean中写入:destroy-method="stop"。stop为类中的销毁方法。
2.在类中实例化DisposableBean接口,重新destroy()方法。
第三种方法:可选择性方法,分为两步
第一步,在xml文件的配置中,配置两个默认方法
default-init-method="defautInit" default-destroy-method="defaultDestroy"
第二步,在类中写相应的方法。
下面分别就三种方法举例:
第一种方法,xml配置。
//1.首先配置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.xsd">
<!-- init-method="start" destroy-method="stop" -->
<bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle" init-method="start" destroy-method="stop" ></bean>
</beans>
//2.在类中写相应的方法
package com.imooc.lifecycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class BeanLifeCycle implements InitializingBean, DisposableBean {
public void start() {
System.out.println("Bean start .");
}
public void stop() {
System.out.println("Bean stop.");
}
}
//3.测试
package com.imooc.test.lifecycle;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.imooc.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanLifecycle extends UnitTestBase {
public TestBeanLifecycle() {
super("classpath:spring-lifecycle.xml");
}
@Test
public void test1() {
super.getBean("beanLifeCycle");
}
}
测试结果为:
8月 01, 2018 2:53:04 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:53:04 CST 2018]; root of context hierarchy
8月 01, 2018 2:53:04 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-lifecycle.xml]
Bean start .
8月 01, 2018 2:53:05 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:53:04 CST 2018]; root of context hierarchy
Bean stop.
可以看到在loading和closing后分别执行了我们写在类中的初始化和destroy方法。
第二种方法:
//1.xml文件不需要写入配置
//2.类中需要实例化两个接口,并且重写方法
package com.imooc.lifecycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class BeanLifeCycle implements InitializingBean, DisposableBean {
@Override
public void destroy() throws Exception {
System.out.println("Bean destroy.");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Bean afterPropertiesSet.");
}
}
运行测试后的结果为:
8月 01, 2018 2:56:46 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:56:46 CST 2018]; root of context hierarchy
8月 01, 2018 2:56:46 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-lifecycle.xml]
Bean afterPropertiesSet.
8月 01, 2018 2:56:46 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:56:46 CST 2018]; root of context hierarchy
Bean destroy.
同样是在loading和closing后分别执行了初始化和destroy方法。
第三种方法:
//1.在xml文件的配置中设置默认的初始化和destroy配置,如倒数第三行所示
<?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.xsd"
default-init-method="defautInit" default-destroy-method="defaultDestroy">
<bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle" ></bean>
</beans>
package com.imooc.lifecycle;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class BeanLifeCycle{
public void defautInit() {
System.out.println("Bean defautInit.");
}
public void defaultDestroy() {
System.out.println("Bean defaultDestroy.");
}
}
测试结果:
8月 01, 2018 2:59:25 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:59:25 CST 2018]; root of context hierarchy
8月 01, 2018 2:59:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-lifecycle.xml]
Bean defautInit.
8月 01, 2018 2:59:25 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 14:59:25 CST 2018]; root of context hierarchy
Bean defaultDestroy.
注意:三个方法各有不同,与方法一和方法二比,方法三是可选择性方法,也就是即使xml文件中写了相应的配置,仍然可以不写方法,不去执行。但是如果按方法一去写bean的配置的话,就必须在类中写相应的方法。
另外如果三个方法同时有的话,默认方法(方法三)不会执行,方法2在方法1之前执行。