java的@PostConstruct注解


轉自:file:///E:/downloads/api/index.html

https://www.cnblogs.com/landiljy/p/5764515.html

javax.annotation 
注釋類型 PostConstruct


@Documented
@Retention(value=RUNTIME)
@Target(value=METHOD)
public @interface PostConstruct

PostConstruct 注釋用於在依賴關系注入完成之后需要執行的方法上,以執行任何初始化。此方法必須在將類放入服務之前調用。支持依賴關系注入的所有類都必須支持此注釋。即使類沒有請求注入任何資源,用 PostConstruct 注釋的方法也必須被調用。只有一個方法可以用此注釋進行注釋。應用 PostConstruct 注釋的方法必須遵守以下所有標准:該方法不得有任何參數,除非是在 EJB 攔截器 (interceptor) 的情況下,根據 EJB 規范的定義,在這種情況下它將帶有一個 InvocationContext 對象 ;該方法的返回類型必須為 void;該方法不得拋出已檢查異常;應用 PostConstruct 的方法可以是 public、protected、package private 或 private;除了應用程序客戶端之外,該方法不能是 static;該方法可以是 final;如果該方法拋出未檢查異常,那么不得將類放入服務中,除非是能夠處理異常並可從中恢復的 EJB。

、、、、、、、、、、、、、、、、、、

Java開發之@PostConstruct和@PreConstruct注解

 

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

使用方式,例如:

復制代碼
1     @PostConstruct                                 //方式1
2     public void someMethod(){
3         ...
4     }
5 
6     public @PostConstruct void someMethod(){        //方式2
7         ...  
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 log
26         // Put your code here
27     }
28 
29     @PostConstruct
30     public void someMethod(){
31         //this.log("執行@PostConstruct修飾的someMethod()方法...");//注意:這樣會出錯
32         System.out.println("時間:"+df.format(new Date())+"執行@PostConstruct修飾的someMethod()方法...");
33     }
34     
35     @PreDestroy
36     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 here
47         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及以上版本才行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM