spring生命周期管理-初始化与销毁

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.如果同时指定两种初始化和销毁方法,初始化接口方法首先运行,参数话初始化方法后运行;销毁时,接口中的销毁方法首先运行,参数指定的销毁方法后运行

  1. 二代码  
  2.   
  3. 1bean  
  4.   
  5. package study.spring;  
  6.   
  7. import org.springframework.beans.factory.InitializingBean;  
  8. import org.springframework.beans.factory.DisposableBean;  
  9.   
  10. public class ContextTest implements InitializingBean,DisposableBean {  
  11.   
  12.     private String content;  
  13.       
  14.     public void init(){ System.out.println("mothed init run."); }  
  15.       
  16.     public void afterPropertiesSet() throws java.lang.Exception{  
  17.         if (content== null) content="hello";  
  18.         System.out.println("afterPropertiesSet run.");  
  19.     }  
  20.       
  21.     public String getContent(){ return content; }  
  22.       
  23.     public void setContent(String content){ this.content = content; }  
  24.       
  25.     public void dispose(){ System.out.println("mothed despose run."); }  
  26.       
  27.     public void destroy(){  
  28.         content = null;  
  29.         System.out.println("destroy run.");  
  30.     }  
  31.       
  32. }  
  33.   
  34. 2测试程序  
  35.   
  36. package study.spring;  
  37.   
  38. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  39. import org.springframework.core.io.FileSystemResource;  
  40. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  
  41.   
  42. public class SimpleInject{  
  43.      
  44.      
  45.      
  46.    public static void main(String[] args){  
  47.     ConfigurableListableBeanFactory beanFactory3 = new XmlBeanFactory(new FileSystemResource("./spring_xml/beans_teacher.xml"));  
  48.     ConfigurableListableBeanFactory beanFactory1 = new XmlBeanFactory(new FileSystemResource("./spring_xml/beans.xml"),beanFactory3);  
  49.     ConfigurableListableBeanFactory beanFactory2 = new XmlBeanFactory(new FileSystemResource("./spring_xml/beans_address.xml"));         
  50.     Runtime.getRuntime().addShutdownHook( new Thread(new ShutdownHook(beanFactory2)));  
  51.     Student student1 = (Student)beanFactory1.getBean("student");  
  52.     ContextTest ct = (ContextTest)beanFactory2.getBean("context");  
  53.     System.out.println(ct.getContent());  
  54.    }  
  55.   
  56. }  
  57.   
  58. class ShutdownHook implements java.lang.Runnable{  
  59.          
  60.        private ConfigurableListableBeanFactory cbf;  
  61.          
  62.        public ShutdownHook(ConfigurableListableBeanFactory cbf){ this.cbf = cbf; }  
  63.          
  64.        public void run(){  
  65.           cbf.destroySingletons();  
  66.        }  
  67.          
  68.    }  
  69.   
  70. 3配置文件  
  71.   
  72. </bean>  
  73. <bean id="context" class="study.spring.ContextTest" init-method="init" destroy-method="dispose">  
  74. </bean> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值