summary of Spring bean lifecycle management

In general, two lifecycle events are particularly relevant to a bean: post-initialization and pre-destruction. Both of these lifecycle events are only fired for beans that are singletons.

1. post-initialization event. 2 ways to implement it: method-based and interface-based.

1) method based. Specify the init method name in the configuration file (init-method="method name").

xml
  1. <bean id="simpleBean1"    
  2.       class="SimpleBean"    
  3.       init-method="init">  
  4.     <property name="age">  
  5.         <value>100value>  
  6.     property>  
  7. bean>  

 

2) interface based. Implement InitializingBean interface and define afterPropertiesSet() method.

java
  1. public class SimpleBeanWithInterface implements InitializingBean {   
  2.        
  3.     public void afterPropertiesSet() throws Exception {   
  4.         System.out.println("Initializing bean");   
  5.     }   
  6. }  

 

2. pre-destruction event. 2 ways to implement it: method-based and interface-based.

1) method based. Specify the destroy mothod in the config file. (destroy-method="method name")

xml
  1. <bean id="destructiveBean"    
  2.       class="DestructiveBean"  
  3.       destroy-method="destroy">  
  4.     <property name="filePath">  
  5.         <value>d:/tmp/test.txtvalue>  
  6.     property>  
  7. bean>  

2) interface based. Implement DisposableBean interface and define destroy() method.

java
  1. public class DestructiveBeanWithInterface implements DisposableBean {   
  2.        
  3.     public void destroy() {   
  4.        
  5.         System.out.println("Destroying Bean");   
  6.     }   
  7. }   

Destruction callbacks are not fired automatically. You need to remember to call destroySingletons() before the application is closed. When the application runs as a servlet, you can simple call the method in the servlet's destroy() method. However, in a stand-alone application, you need to take advantage of the Java's shutdown hook, a thread executed just before the application shuts down.

java
  1. public class ShutdownHook implements Runnable {   
  2.        
  3.     private ConfigurableListableBeanFactory factory;   
  4.        
  5.     public ShutdownHook(ConfigurableListableBeanFactory factory) {   
  6.         this.factory = factory;   
  7.     }   
  8.        
  9.     public void run() {   
  10.         System.out.println("Destroying Singletons");   
  11.         factory.destroySingletons();   
  12.         System.out.println("Singletons Destroyed");   
  13.     }   
  14.        
  15. }  

Register this Thread as a shutdown hook using Runtime.addShutdownHook(). 

java
  1. public class ShutdownHookExample {   
  2.        
  3.     public static void main(String[] args) {   
  4.         ConfigurableListableBeanFactory factory = new XmlBeanFactory(   
  5.                 new FileSystemResource(   
  6.                         "./disposeInterface.xml"));   
  7.        
  8.         Runtime.getRuntime().addShutdownHook(   
  9.                 new Thread(new ShutdownHook(factory)));   
  10.         DestructiveBeanWithInterface bean =     
  11.         (DestructiveBeanWithInterface)factory.getBean("destructiveBean");   
  12.     }   
  13. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值