Spring bean 通過實現 InitializingBean ,DisposableBean 介面實現初始化方法和銷燬前操作

工程師WWW發表於2015-11-09

關於在spring  容器初始化 bean 和銷燬前所做的操作定義方式有三種:

第一種:通過@PostConstruct 和 @PreDestroy 方法 實現初始化和銷燬bean之前進行的操作

第二種是:通過 在xml中定義init-method 和  destory-method方法

第三種是:通過bean實現InitializingBean和DisposableBean介面


1:定義相應類實現InitializingBean ,DisposableBean 介面

[java] view plaincopy
  1. package com.myapp.core.annotation.init;  
  2.   
  3. import javax.annotation.PostConstruct;  
  4. import javax.annotation.PreDestroy;  
  5.   
  6. import org.springframework.beans.factory.DisposableBean;  
  7. import org.springframework.beans.factory.InitializingBean;  
  8.   
  9. public class PersonService  implements InitializingBean,DisposableBean{  
  10.     
  11.     private String  message;  
  12.   
  13.     public String getMessage() {  
  14.         return message;  
  15.     }  
  16.   
  17.     public void setMessage(String message) {  
  18.         this.message = message;  
  19.     }  
  20.   
  21.     @Override  
  22.     public void destroy() throws Exception {  
  23.         // TODO Auto-generated method stub  
  24.         System.out.println("I'm  init  method  using implements InitializingBean interface...."+message);  
  25.           
  26.     }  
  27.   
  28.     @Override  
  29.     public void afterPropertiesSet() throws Exception {  
  30.         // TODO Auto-generated method stub  
  31.         System.out.println("I'm  init  method  using implements DisposableBean interface...."+message);  
  32.           
  33.     }  
  34.       
  35.   
  36. }  

2:定義相應的配置檔案:

[java] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4. xmlns:context="http://www.springframework.org/schema/context"  
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  7. http://www.springframework.org/schema/context  
  8. http://www.springframework.org/schema/context/spring-context-3.1.xsd">  
  9.   
  10. <!-- <context:component-scan  base-package="com.myapp.core.jsr330"/> -->  
  11.   
  12. <!-- <context:annotation-config /> -->  
  13.   
  14.   
  15. <!-- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />  
  16. <bean id="personService" class="com.myapp.core.annotation.init.PersonService">  
  17.           <property name="message" value="123"></property>  
  18. </bean>  
  19.  -->  
  20.   
  21. <bean id="personService" class="com.myapp.core.annotation.init.PersonService">  
  22.           <property name="message" value="123"></property>  
  23. </bean>  
  24.   
  25. </beans>  

3:測試類:

[java] view plaincopy
  1. package com.myapp.core.annotation.init;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.AbstractApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class MainTest {  
  8.       
  9.     public static void main(String[] args) {  
  10.           
  11.         AbstractApplicationContext  context = new ClassPathXmlApplicationContext("resource/annotation.xml");  
  12.           
  13.         PersonService   personService  =  (PersonService)context.getBean("personService");  
  14.           
  15.              context.registerShutdownHook();  
  16.     }  
  17.   
  18. }  

4:輸出測試結果:

[plain] view plaincopy
  1. I'm  init  method  using implements DisposableBean interface....123  
  2. 三月 16, 2013 5:06:34 下午 org.springframework.context.support.AbstractApplicationContext doClose  
  3. INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@205756: startup date [Sat Mar 16 17:06:30 CST 2013]; root of context hierarchy  
  4. I'm  init  method  using implements InitializingBean interface....123  


over

相關文章