1. 程式人生 > >Java開發之Tomcat註解@PostConstruct和@PreConstruct註解

Java開發之Tomcat註解@PostConstruct和@PreConstruct註解

當我們啟動專案想要初始化一些初始化資料時可以用PostConstruct註解

本文轉載自https://www.cnblogs.com/landiljy/p/5764515.html

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

使用方式,例如:


@PostConstruct                                 //方式1
     public void someMethod(){
         ...
     }
 
     public @PostConstruct void someMethod(){        //方式2
         ...  
     }

[email protected]說明

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

[email protected]說明

     被@PreDestroy修飾的方法會在伺服器解除安裝Servlet的時候執行,並且只會被伺服器呼叫一次,類似於Servlet的destroy()方法。被@PreDestroy修飾的方法會在destroy()方法之後執行,在Servlet被徹底解除安裝之前。

(詳見下面的程式實踐)

3.程式實踐

web.xml

<!-- @PostConstruct和@PreDestroy註解 -->
  <servlet>
    <servlet-name>AnnotationServlet</servlet-name>
    <servlet-class>com.servlet.AnnotationServlet</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>AnnotationServlet</servlet-name>
    <url-pattern>/servlet/AnnotationServlet</url-pattern>
  </servlet-mapping>


以下是Servlet實現的,也可以是Spring註解形式

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AnnotationServlet extends HttpServlet {
    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");//設定日期格式,精確到毫秒
    
    public AnnotationServlet(){
        System.out.println("時間:"+df.format(new Date())+"執行建構函式...");
    }
    
    public void destroy() {
        this.log("時間:"+df.format(new Date())+"執行destroy()方法...");
        //super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    @PostConstruct
    public void someMethod(){
        //this.log("執行@PostConstruct修飾的someMethod()方法...");//注意:這樣會出錯
        System.out.println("時間:"+df.format(new Date())+"執行@PostConstruct修飾的someMethod()方法...");
    }
    
    @PreDestroy
    public void otherMethod(){
        System.out.println("時間:"+df.format(new Date())+"執行@PreDestroy修飾的otherMethod()方法...");
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.log("時間:"+df.format(new Date())+"執行doGet()方法...");
    }

    public void init() throws ServletException {
        // Put your code here
        this.log("時間:"+df.format(new Date())+"執行init()方法...");
    }
    
    protected void service(HttpServletRequest request, HttpServletResponse response)
               throws ServletException, IOException{
        this.log("時間:"+df.format(new Date())+"執行service()方法...");
        super.service(request, response);
    }

}

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