JAVA註解之@PostConstruct和@PreConstruct

Java EE 5規範開始,Servlet中增加了兩個影響Servlet生命週期的註解(Annotion);@PostConstruct和@PreDestroy。這兩個註解被用來修飾一個非靜態的void()方法 。寫法有如下兩種方式:

@PostConstruct

Public void someMethod() {}
                                                                                    
或者

public @PostConstruct void someMethod(){}

    被@PostConstruct修飾的方法會在服務器加載Servle的時候運行,並且只會被服務器執行一次。PostConstruct在構造函數之後執行,init()方法之前執行。PreDestroy()方法在destroy()方法執行執行之後執行

 

 

 

被註解的Servlet生命週期

    需要注意的是,註解會多多少少地影響到服務器的啓動速度。服務器在啓動時候會遍歷Web 應用的WEB-INF/classes下的所有class文件與WEB-INF/lib下的所有jar文件,以檢查哪些類使用了註解。如果應用程序中沒有 使用任何註解,可以在Web.xml中設置的metadata-complete屬性爲true.(支持@PostConstruct和 @PreDestroy的服務器需要支持Servlet2.5規範。Tomcat5.x僅支持Servlet2.4規範。)


我現在要說的是用實例說明它有什麼作用。

比如說我有一種情況,在我的servlet初始化加載之前我想處理一些東西,像加載緩存等等。

怎麼做。@PostConstruct就派上用場了。那爲什麼這玩意用的不多呢,這是因爲如果初始化之前我們要加載或處理某些玩意完全可以在構造器初始化時就處理了,但這種方法需要自己重寫構造器。好吧。直接上代碼看看具體用它的時候怎麼做的。

[java] view plain copy
 print?
  1. package com.whaty.products.whatysns.web.info;  
  2.   
  3. import javax.annotation.PostConstruct;  
  4. import javax.annotation.Resource;  
  5.   
  6. import org.springframework.stereotype.Service;  
  7. import org.springframework.util.Assert;  
  8.   
  9. import com.whaty.framework.cache.core.model.Cache;  
  10. import com.whaty.framework.cache.core.service.CacheService;  
  11. import com.whaty.framework.cache.entitycache.service.EntityCacheHelper;  
  12. import com.whaty.framework.cache.entitycache.service.IEntityDaoAdapter;  
  13.   
  14. /** 
  15.  * @author bc_qi 
  16.  * @param <KEY> 
  17.  * @param <ENTITY> 
  18.  */  
  19. @Service("AjaxCacheableService")  
  20. public class AjaxCacheableService{  
  21.       
  22.     @Resource(name="cacheService")  
  23.     protected CacheService cacheService;  
  24.       
  25.     protected boolean useReadWriteEntityDao = false;  
  26.     protected boolean useCache = true;  
  27.     protected int entityCacheMaxSize = 1000;  
  28.     protected int entityCacheMaxLiveSeconds = 3600;  
  29.     protected Cache entityCache;  
  30.       
  31.       
  32.     /** 
  33.      * 構造方法執行後,初始化, 
  34.      */  
  35.     @PostConstruct  
  36.     public void init() {  
  37.         Assert.notNull(cacheService, "cacheService must be set!");  
  38.         getCache();  
  39.     }  
  40.   
  41.     /** 
  42.      * 獲取cache 
  43.      * @return 
  44.      */  
  45.     protected Cache getCache() {  
  46.         if (entityCache == null) {  
  47.             entityCache = cacheService.addCache(this.getClass().getName(),entityCacheMaxLiveSeconds);  
  48.         }  
  49.         return entityCache;  
  50.     }  
  51.       
  52.     /** 
  53.      * @param id 
  54.      * @param useCache 是否使用Cache 
  55.      * @return 
  56.      */  
  57.     public Object getCache(String id) {  
  58.         String strid = String.valueOf(id);  
  59.         Object o = entityCache.get(strid);  
  60.         return  o;  
  61.     }  
  62.       
  63.     public Object putCache(int tTLSeconds,String cacheId,Object value) {  
  64.         String strid = String.valueOf(cacheId);  
  65.         Object o = entityCache.get(strid);  
  66.         if (o != null) {  
  67.             return  o;  
  68.         } else {  
  69.             entityCache.put(strid, value, tTLSeconds);  
  70.             return value;  
  71.         }  
  72.     }  
  73.       
  74. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章