@postconstruct and @predestroy annotations for Java development

Source: Internet
Author: User

Starting with the Java EE5 specification, the servlet adds two annotations (Annotation) that affect the servlet life cycle: @PostConstruct and @predestroy. These two annotations are used to decorate a non-static void () method . And this method cannot have a throw exception declaration.

How to use, for example:

1 @PostConstruct                                 // Way 12publicvoid  SomeMethod () {  3     ... 4 }56publicvoid SomeMethod () {        // mode 2      7   ... 8 }

[Email protected] Description

The method that is modified by @postconstruct will run when the server loads the servlet and will only be called once by the server, similar to the Serclet Inti () method. Methods that are modified by @postconstruct will run before the Init () method after the constructor.

[Email protected] Description

The @predestroy-modified method runs when the server unloads the servlet and is only called once by the server, similar to the Destroy () method of the servlet. Methods that are modified by @predestroy are run after the Destroy () method, before the servlet is completely uninstalled. (See the program practice below)

3. Procedure Practice

Xml

1 <!--@PostConstruct and @predestroy annotations--2 <servlet>3     <servlet-name >AnnotationServlet</servlet-name>4     <servlet-class> com.servlet.annotationservlet</servlet-class>5 </servlet>6 < servlet-mapping>7     <servlet-name>AnnotationServlet</servlet-name>8     <url-pattern>/servlet/AnnotationServlet</url-pattern>9 </servlet-mapping>
1  PackageCom.servlet;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 ImportJava.sql.Time;6 ImportJava.text.SimpleDateFormat;7 Importjava.util.Date;8 9 Importjavax.annotation.PostConstruct;Ten ImportJavax.annotation.PreDestroy; One Importjavax.servlet.ServletException; A ImportJavax.servlet.http.HttpServlet; - Importjavax.servlet.http.HttpServletRequest; - ImportJavax.servlet.http.HttpServletResponse; the  -  Public classAnnotationservletextendsHttpServlet { -SimpleDateFormat DF =NewSimpleDateFormat ("HH:mm:ss. SSS ");//set the date format to be accurate to milliseconds -      +      PublicAnnotationservlet () { -System.out.println ("Time:" +df.format (NewDate ()) + "Execute constructor ..."); +     } A      at      Public voiddestroy () { -          This. log ("Time:" +df.format (NewDate ()) + "Execute Destroy () method ..."); -         //Super.destroy ();//Just puts "destroy" string in log -         //Put Your code here -     } -  in @PostConstruct -      Public voidSomeMethod () { to         //This.log ("Perform @postconstruct-Modified SomeMethod () method ...");//Note: This can be an error +System.out.println ("Time:" +df.format (NewDate ()) + "SomeMethod () method for performing @postconstruct adornments ..."); -     } the      * @PreDestroy $      Public voidOthermethod () {Panax NotoginsengSystem.out.println ("Time:" +df.format (NewDate ()) + "Othermethod () method for performing @predestroy adornments ..."); -     } the      +      Public voiddoget (httpservletrequest request, httpservletresponse response) A             throwsservletexception, IOException { the          This. log ("Time:" +df.format (NewDate ()) + "Execute doget () method ..."); +     } -  $      Public voidInit ()throwsservletexception { $         //Put Your code here -          This. log ("Time:" +df.format (NewDate ()) + "Execute init () method ..."); -     } the      -     protected voidService (HttpServletRequest request, httpservletresponse response)Wuyi                throwsservletexception, ioexception{ the          This. log ("Time:" +df.format (NewDate ()) + "Execute service () method ..."); -         Super. Service (request, response); Wu     } -  About}

Operation Result:

4. Precautions

The number of annotations can affect the server's startup speed. When the server starts, it 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 program, you can set the <web-app> Metadatacomplete property to True in Web. Xml to turn off routine checks at server startup.

Servers that support annotations need to support Servlet2.5 and above, so Tomcat requires 6.0.X and later.

@postconstruct and @predestroy annotations for Java development

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.