從JavaEE5規範開始,Servlet增加了兩個影響Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct。這兩個注解被用來修飾一個非靜态的void()方法.而且這個方法不能有抛出異常聲明。
@PostContruct是spring架構的注解,在方法上加該注解會在項目啟動的時候執行該方法,也可以了解為在spring容器初始化的時候執行該方法。
@PostConstruct在項目中的用處1.spring項目加載資料字典
@PostConstruct注解的方法在項目啟動的時候執行這個方法,也可以了解為在spring容器啟動的時候執行,可作為一些資料的正常化加載,比如資料字典之類的。
2.spring項目的定時任務
spring自帶的@schedule,沒有開關,項目啟動總會啟動一個線程;做項目的時候就使用Java的timer,這個設定開關即可自由的控制,關閉的時候,不會啟動線程;Java的timer也需要找到一個啟動類,可以放到main函數裡面啟動,這樣的話,代碼的耦合性太高了,而使用PostConstruct是很幹淨的。
spring中Constructor、@Autowired、@PostConstruct的順序
其實從依賴注入的字面意思就可以知道,要将對象p注入到對象a,那麼首先就必須得生成對象p與對象a,才能執行注入。是以,如果一個類A中有個成員變量p被@Autowired注解,那麼@Autowired注入是發生在A的構造方法執行完之後的。
如果想在生成對象時候完成某些初始化操作,而偏偏這些初始化操作又依賴于依賴注入,那麼就無法在構造函數中實作。為此,可以使用@PostConstruct注解一個方法來完成初始化,@PostConstruct注解的方法将會在依賴注入完成後被自動調用。
Constructor >> @Autowired >> @PostConstruct
特點:
@PostConstruct的使用1、隻有一個非靜态方法能使用此注解
2、被注解的方法不得有任何參數
3、被注解的方法傳回值必須為void
4、被注解方法不得抛出已檢查異常
5、此方法隻會被執行一次
從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就派上用場了。那為什麼這玩意用的不多呢,這是因為如果初始化之前我們要加載或處理某些玩意完全可以在構造器初始化時就處理了,但這種方法需要自己重寫構造器。好吧。直接上代碼看看具體用它的時候怎麼做的。
package com.whaty.products.whatysns.web.info;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import com.whaty.framework.cache.core.model.Cache;
import com.whaty.framework.cache.core.service.CacheService;
import com.whaty.framework.cache.entitycache.service.EntityCacheHelper;
import com.whaty.framework.cache.entitycache.service.IEntityDaoAdapter;
/**
* @author bc_qi
* @param <KEY>
* @param <ENTITY>
*/
@Service("AjaxCacheableService")
public class AjaxCacheableService{
@Resource(name="cacheService")
protected CacheService cacheService;
protected boolean useReadWriteEntityDao = false;
protected boolean useCache = true;
protected int entityCacheMaxSize = 1000;
protected int entityCacheMaxLiveSeconds = 3600;
protected Cache entityCache;
/**
* 構造方法執行後,初始化,
*/
@PostConstruct
public void init() {
Assert.notNull(cacheService, "cacheService must be set!");
getCache();
}
/**
* 擷取cache
* @return
*/
protected Cache getCache() {
if (entityCache == null) {
entityCache = cacheService.addCache(this.getClass().getName(),entityCacheMaxLiveSeconds);
}
return entityCache;
}
/**
* @param id
* @param useCache 是否使用Cache
* @return
*/
public Object getCache(String id) {
String strid = String.valueOf(id);
Object o = entityCache.get(strid);
return o;
}
public Object putCache(int tTLSeconds,String cacheId,Object value) {
String strid = String.valueOf(cacheId);
Object o = entityCache.get(strid);
if (o != null) {
return o;
} else {
entityCache.put(strid, value, tTLSeconds);
return value;
}
}
}