Java開發之@PostConstruct和@PreConstruct註解,postconstruct註解

來源:互聯網
上載者:User

Java開發之@PostConstruct和@PreConstruct註解,postconstruct註解

      從Java EE5規範開始,Servlet增加了兩個影響Servlet生命週期的註解(Annotation):@PostConstruct和@PreConstruct。這兩個註解被用來修飾一個非靜態void()方法.而且這個方法不能有拋出異常聲明。

使用方式,例如:

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

1.@PostConstruct說明

     被@PostConstruct修飾的方法會在伺服器載入Servlet的時候運行,並且只會被伺服器調用一次,類似於Serclet的inti()方法。被@PostConstruct修飾的方法會在建構函式之後,init()方法之前運行。

2.@PreConstruct說明

     被@PreConstruct修飾的方法會在伺服器卸載Servlet的時候運行,並且只會被伺服器調用一次,類似於Servlet的destroy()方法。被@PreConstruct修飾的方法會在destroy()方法之後運行,在Servlet被徹底卸載之前。(詳見下面的程式實踐)

3.程式實踐

web.xml

1 <!-- @PostConstruct和@PreDestroy註解 -->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");//設定日期格式,精確到毫秒18     19     public AnnotationServlet(){20         System.out.println("時間:"+df.format(new Date())+"執行建構函式...");21     }22     23     public void destroy() {24         this.log("時間:"+df.format(new Date())+"執行destroy()方法...");25         //super.destroy(); // Just puts "destroy" string in log26         // Put your code here27     }28 29     @PostConstruct30     public void someMethod(){31         //this.log("執行@PostConstruct修飾的someMethod()方法...");//注意:這樣會出錯32         System.out.println("時間:"+df.format(new Date())+"執行@PostConstruct修飾的someMethod()方法...");33     }34     35     @PreDestroy36     public void otherMethod(){37         System.out.println("時間:"+df.format(new Date())+"執行@PreDestroy修飾的otherMethod()方法...");38     }39     40     public void doGet(HttpServletRequest request, HttpServletResponse response)41             throws ServletException, IOException {42         this.log("時間:"+df.format(new Date())+"執行doGet()方法...");43     }44 45     public void init() throws ServletException {46         // Put your code here47         this.log("時間:"+df.format(new Date())+"執行init()方法...");48     }49     50     protected void service(HttpServletRequest request, HttpServletResponse response)51                throws ServletException, IOException{52         this.log("時間:"+df.format(new Date())+"執行service()方法...");53         super.service(request, response);54     }55 56 }

運行結果:

4.注意事項

     註解多少會影響伺服器的啟動速度。伺服器在啟動的時候,會遍曆Web應用的WEB-INF/classes下的所有class檔案與WEB-INF/lib下的所有jar檔案,以檢查哪些類使用了註解。如果程式中沒有使用任何註解,可以在web.xml中設定<web-app>的metadatacomplete屬性為true來關掉伺服器啟動時的例行檢查。

     

      支援註解的伺服器需要支援到Servlet2.5及以上規範,所以Tomcat要6.0.X及以上版本才行。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.