spring生命周期管理-初始化与销毁
2013-04-04 14:33
465 查看
spring生命周期管理只涉及单例对象的管理
一初始化与销毁
1.通过设置bean的init-mothed属性指定初始化的方法,他的限制是方法无法接受任何参数,方法可以为static
2.实现InitializingBean接口的afterPropertiesSet()方法
3.最好的方法是在afterProptertiesSet()中调用自定义初始化方法
4.销毁单例对象可以通过指定bean的destroy-method属性,指定销毁时执行的方法名
5.销毁单例对象可以通过实现DisposableBean的destroy实现
6.销毁方法的触发是通过生成该单例对象的BeanFactory的destroySingletons方法的执行而触发
7.通过指定 ShutdownHook可以指定一个线程来执行BeanFactory的destroySingletons方法
8.如果同时指定两种初始化和销毁方法,初始化接口方法首先运行,参数话初始化方法后运行;销毁时,接口中的销毁方法首先运行,参数指定的销毁方法后运行
[java]
view plaincopy
二代码
1bean
package study.spring;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.DisposableBean;
public class ContextTest implements InitializingBean,DisposableBean {
private String content;
public void init(){ System.out.println("mothed init run."); }
public void afterPropertiesSet() throws java.lang.Exception{
if (content== null) content="hello";
System.out.println("afterPropertiesSet run.");
}
public String getContent(){ return content; }
public void setContent(String content){ this.content = content; }
public void dispose(){ System.out.println("mothed despose run."); }
public void destroy(){
content = null;
System.out.println("destroy run.");
}
}
2测试程序
package study.spring;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class SimpleInject{
public static void main(String[] args){
ConfigurableListableBeanFactory beanFactory3 = new XmlBeanFactory(new FileSystemResource("./spring_xml/beans_teacher.xml"));
ConfigurableListableBeanFactory beanFactory1 = new XmlBeanFactory(new FileSystemResource("./spring_xml/beans.xml"),beanFactory3);
ConfigurableListableBeanFactory beanFactory2 = new XmlBeanFactory(new FileSystemResource("./spring_xml/beans_address.xml"));
Runtime.getRuntime().addShutdownHook( new Thread(new ShutdownHook(beanFactory2)));
Student student1 = (Student)beanFactory1.getBean("student");
ContextTest ct = (ContextTest)beanFactory2.getBean("context");
System.out.println(ct.getContent());
}
}
class ShutdownHook implements java.lang.Runnable{
private ConfigurableListableBeanFactory cbf;
public ShutdownHook(ConfigurableListableBeanFactory cbf){ this.cbf = cbf; }
public void run(){
cbf.destroySingletons();
}
}
3配置文件
</bean>
<bean id="context" class="study.spring.ContextTest" init-method="init" destroy-method="dispose">
</bean>
一初始化与销毁
1.通过设置bean的init-mothed属性指定初始化的方法,他的限制是方法无法接受任何参数,方法可以为static
2.实现InitializingBean接口的afterPropertiesSet()方法
3.最好的方法是在afterProptertiesSet()中调用自定义初始化方法
4.销毁单例对象可以通过指定bean的destroy-method属性,指定销毁时执行的方法名
5.销毁单例对象可以通过实现DisposableBean的destroy实现
6.销毁方法的触发是通过生成该单例对象的BeanFactory的destroySingletons方法的执行而触发
7.通过指定 ShutdownHook可以指定一个线程来执行BeanFactory的destroySingletons方法
8.如果同时指定两种初始化和销毁方法,初始化接口方法首先运行,参数话初始化方法后运行;销毁时,接口中的销毁方法首先运行,参数指定的销毁方法后运行
[java]
view plaincopy
二代码
1bean
package study.spring;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.DisposableBean;
public class ContextTest implements InitializingBean,DisposableBean {
private String content;
public void init(){ System.out.println("mothed init run."); }
public void afterPropertiesSet() throws java.lang.Exception{
if (content== null) content="hello";
System.out.println("afterPropertiesSet run.");
}
public String getContent(){ return content; }
public void setContent(String content){ this.content = content; }
public void dispose(){ System.out.println("mothed despose run."); }
public void destroy(){
content = null;
System.out.println("destroy run.");
}
}
2测试程序
package study.spring;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class SimpleInject{
public static void main(String[] args){
ConfigurableListableBeanFactory beanFactory3 = new XmlBeanFactory(new FileSystemResource("./spring_xml/beans_teacher.xml"));
ConfigurableListableBeanFactory beanFactory1 = new XmlBeanFactory(new FileSystemResource("./spring_xml/beans.xml"),beanFactory3);
ConfigurableListableBeanFactory beanFactory2 = new XmlBeanFactory(new FileSystemResource("./spring_xml/beans_address.xml"));
Runtime.getRuntime().addShutdownHook( new Thread(new ShutdownHook(beanFactory2)));
Student student1 = (Student)beanFactory1.getBean("student");
ContextTest ct = (ContextTest)beanFactory2.getBean("context");
System.out.println(ct.getContent());
}
}
class ShutdownHook implements java.lang.Runnable{
private ConfigurableListableBeanFactory cbf;
public ShutdownHook(ConfigurableListableBeanFactory cbf){ this.cbf = cbf; }
public void run(){
cbf.destroySingletons();
}
}
3配置文件
</bean>
<bean id="context" class="study.spring.ContextTest" init-method="init" destroy-method="dispose">
</bean>
相关文章推荐
- spring生命周期管理-初始化与销毁
- spring生命周期管理-初始化与销毁
- 【Spring学习21】Bean生命周期回调:初始化回调和销毁回调
- 【Spring】Spring常用配置-Bean的初始化和销毁(生命周期)
- spring boot 管理bean初始化方法,以及销毁方法
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- spring: bean初始化和销毁方法
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- Spring中Bean的生命周期管理
- tomcat系列分析之生命周期管理初始化动作
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring 自定义对象初始化及销毁
- spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用
- Spring管理bean的生命周期
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- Spring管理Bean的生命周期
- Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- spring管理bean生命周期
- 在Spring容器中Bean的几种初始化、销毁方法的实现和先后顺序