Starting with the Java EE 5 specification, the servlet adds two annotations (annotion) that affect the servlet life cycle, @PostConstruct and @predestroy. These two annotations are used to decorate a non-static void () method. There are two ways of writing:
void SomeMethod () {}
Or
Public void SomeMethod () {}
The method that is modified by @postconstruct will run when the server loads Servle and will only be executed once by the server. Postconstruct executes after the constructor, before the Init () method. The Predestroy () method executes after the Destroy () method executes
Annotated servlet life cycle
It is important to note that annotations can affect the server's startup speed more or less. At boot time, the server traverses all the class files under the Web app's web-inf/classes and all the jar files under Web-inf/lib to check which classes use annotations. If no annotations are used in the application, the Metadata-complete property that can be set in Web. XML is true. (Servers that support @postconstruct and @PreDestroy need to support the Servlet2.5 specification.) tomcat5.x only supports the Servlet2.4 specification. )
What I'm going to say now is to use an example to illustrate its usefulness.
For example, I have a situation where I want to deal with something like loading a cache and so on before my servlet initializes the load.
What do you do? @PostConstruct comes in handy. Well, that's not a lot of stuff, because if we were to load or manipulate something before it was initialized, it could be handled completely at the time the constructor was initialized, but this method would have to rewrite the constructor itself. All right. Go directly to the code and see how it's done with it.
PackageCom.whaty.products.whatysns.web.info; Importjavax.annotation.PostConstruct; ImportJavax.annotation.Resource; ImportOrg.springframework.stereotype.Service; ImportOrg.springframework.util.Assert; ImportCom.whaty.framework.cache.core.model.Cache; ImportCom.whaty.framework.cache.core.service.CacheService; ImportCom.whaty.framework.cache.entitycache.service.EntityCacheHelper; ImportCom.whaty.framework.cache.entitycache.service.IEntityDaoAdapter; /** * @authorBc_qi *@param<KEY> *@param<ENTITY>*/@Service ("Ajaxcacheableservice") Public classajaxcacheableservice{@Resource (name= "Cacheservice") protectedCacheservice Cacheservice; protected BooleanUsereadwriteentitydao =false; protected BooleanUseCache =true; protected intentitycachemaxsize = 1000; protected intEntitycachemaxliveseconds = 3600; protectedCache Entitycache; /*** After the construction method is executed, initialize,*/@PostConstruct Public voidinit () {assert.notnull (Cacheservice,"Cacheservice must be set!"); GetCache (); } /*** Get Cache *@return */ protectedCache GetCache () {if(Entitycache = =NULL) {Entitycache= Cacheservice.addcache ( This. GetClass (). GetName (), entitycachemaxliveseconds); } returnEntitycache; } /** * @paramID *@paramUseCache whether to use the cache *@return */ PublicObject GetCache (String id) {string Strid=string.valueof (ID); Object o=Entitycache.get (Strid); returno; } PublicObject Putcache (intttlseconds,string cacheid,object value) {String Strid=string.valueof (CacheID); Object o=Entitycache.get (Strid); if(O! =NULL) { returno; } Else{entitycache.put (Strid, value, ttlseconds); returnvalue; } } }
This article transferred from: http://blog.csdn.net/yaerfeng/article/details/8447530
Annotated @postconstruct and @predestroy explanation and examples