Annotated @postconstruct and @predestroy explanation and examples

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.