Java Development @ PostConstruct and @ PreConstruct annotations, postconstruct annotations

Source: Internet
Author: User

Java Development @ PostConstruct and @ PreConstruct annotations, postconstruct annotations

Starting from the Java EE5 specification, Servlet adds two annotations (Annotation) that affect the Servlet lifecycle: @ PostConstruct and @ PreConstruct. These two annotations are used to modify a non-static void () method, and this method cannot throw an exception declaration.

Usage, for example:

1 @ PostConstruct // Method 12 public void someMethod () {3... 4} 5 6 public @ PostConstruct void someMethod () {// Method 27... 8}

1. @ PostConstruct description

The method 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. The method modified by @ PostConstruct runs before the init () method after the constructor.

2. @ PreConstruct description

The method modified by @ PreConstruct runs when the server detaches the Servlet and is called only once by the server, similar to the destroy () method of Servlet. The method modified by @ PreConstruct will run after the destroy () method before the Servlet is completely uninstalled. (For details, see the following program practices)

3. Program practice

Web. xml

1 <! -- @ PostConstruct and @ PreDestroy annotation --> 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>
AnnotationServlet
1 package com. servlet; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 import java. SQL. time; 6 import java. text. simpleDateFormat; 7 import java. util. date; 8 9 import javax. annotation. postConstruct; 10 import javax. annotation. preDestroy; 11 import javax. servlet. servletException; 12 import javax. servlet. http. httpServlet; 13 import javax. servlet. http. httpServletRequest; 14 import javax. servlet. http. httpServletResponse; 15 16 public class AnnotationServlet extends HttpServlet {17 SimpleDateFormat df = new SimpleDateFormat ("HH: mm: ss. SSS "); // set the date format, accurate to 18 ms 19 public AnnotationServlet () {20 System. out. println ("time:" + df. format (new Date () + "execute the constructor... "); 21} 22 23 public void destroy () {24 this. log ("time:" + df. format (new Date () + "execute the destroy () method... "); 25 // super. destroy (); // Just puts "destroy" string in log26 // Put your code here27} 28 29 @ PostConstruct30 public void someMethod () {31 // this. log ("execute @ PostConstruct modified someMethod () method... "); // Note: 32 System errors will occur. out. println ("time:" + df. format (new Date () + "execute the someMethod () method modified by @ PostConstruct... "); 33} 34 35 @ PreDestroy36 public void otherMethod () {37 System. out. println ("time:" + df. format (new Date () + "execute the otherMethod () method modified by @ PreDestroy... "); 38} 39 40 public void doGet (HttpServletRequest request, HttpServletResponse response) 41 throws ServletException, IOException {42 this. log ("time:" + df. format (new Date () + "execute the doGet () method... "); 43} 44 45 public void init () throws ServletException {46 // Put your code here47 this. log ("time:" + df. format (new Date () + "execute the init () method... "); 48} 49 50 protected void service (HttpServletRequest request, HttpServletResponse response) 51 throws ServletException, IOException {52 this. log ("time:" + df. format (new Date () + "run the service () method... "); 53 super. service (request, response); 54} 55 56}

Running result:

4. Notes

Note how much will affect the server startup speed. When the server is started, it will traverse all the class files under the WEB-INF/classes of the Web application and all jar files under the WEB-INF/lib to check which classes use the annotation. If no annotation is used in the program, you can set the metadatacomplete attribute of <web-app> to true in web. xml to disable the routine check during server startup.

Servers that support annotation must support Servlet2.5 and later specifications, so Tomcat must be 6.0.X or later.

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.