`
SilverRing
  • 浏览: 71332 次
社区版块
存档分类
最新评论

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. }  
分享到:
评论
1 楼 SilverRing 2007-04-04  
Another method of implementing shutdown hook is to let the class implement BeanFactoryAware interface.

public class ShutdownHookBean implements BeanFactoryAware,
        Runnable {
    
    private ConfigurableListableBeanFactory factory;
    
    public void setBeanFactory(BeanFactory factory) throws BeansException {
    
        if (factory instanceof ConfigurableListableBeanFactory) {
            this.factory = (ConfigurableListableBeanFactory) factory;
            Runtime.getRuntime().addShutdownHook(new Thread(this));
        }
    }
    
    public void run() {
        if (factory != null) {
            System.out.println("Destroying Singletons");
            factory.destroySingletons();
            System.out.println("Singletons Destroyed");
        }
    }
}


And the main method should be like this:

    public static void main(String[] args) {
        ConfigurableListableBeanFactory factory = new XmlBeanFactory(
                new FileSystemResource(
                        "./shutdownHook.xml"));
    
        // make sure the shutdown hook is created
        factory.preInstantiateSingletons();
    
        DestructiveBeanWithInterface bean =  
        (DestructiveBeanWithInterface)factory.getBean("destructiveBean");
    }


Note the call to ConfigurableListableBeanFactory.preInstantiateSingletons(). By default, Spring lazily instantiates singleton beans as they are needed. Invoking preInstantiateSingletons() causes Spring to run through all its singleton bean definitions and create the instances.

相关推荐

Global site tag (gtag.js) - Google Analytics