從Java EE5規(guī)范開始,Servlet增加了兩個(gè)影響Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct。這兩個(gè)注解被用來修飾一個(gè)非靜態(tài)的void()方法.而且這個(gè)方法不能有拋出異常聲明。
使用方式,例如:
1 @PostConstruct //方式12 public void someMethod(){3 ...4 }5 6 public @PostConstruct void someMethod(){ //方式27 ... 8 }
1.@PostConstruct說明
被@PostConstruct修飾的方法會(huì)在服務(wù)器加載Servlet的時(shí)候運(yùn)行,并且只會(huì)被服務(wù)器調(diào)用一次,類似于Serclet的inti()方法。被@PostConstruct修飾的方法會(huì)在構(gòu)造函數(shù)之后,init()方法之前運(yùn)行。
2.@PreConstruct說明
被@PreConstruct修飾的方法會(huì)在服務(wù)器卸載Servlet的時(shí)候運(yùn)行,并且只會(huì)被服務(wù)器調(diào)用一次,類似于Servlet的destroy()方法。被@PreConstruct修飾的方法會(huì)在destroy()方法之后運(yùn)行,在Servlet被徹底卸載之前。(詳見下面的程序?qū)嵺`)
3.程序?qū)嵺`
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");//設(shè)置日期格式,精確到毫秒18 19 public AnnotationServlet(){20 System.out.println("時(shí)間:"+df.format(new Date())+"執(zhí)行構(gòu)造函數(shù)...");21 }22 23 public void destroy() {24 this.log("時(shí)間:"+df.format(new Date())+"執(zhí)行destroy()方法...");25 //super.destroy(); // Just puts "destroy" string in log26 // Put your code here27 }28 29 @PostConstruct30 public void someMethod(){31 //this.log("執(zhí)行@PostConstruct修飾的someMethod()方法...");//注意:這樣會(huì)出錯(cuò)32 System.out.println("時(shí)間:"+df.format(new Date())+"執(zhí)行@PostConstruct修飾的someMethod()方法...");33 }34 35 @PreDestroy36 public void otherMethod(){37 System.out.println("時(shí)間:"+df.format(new Date())+"執(zhí)行@PreDestroy修飾的otherMethod()方法...");38 }39 40 public void doGet(HttpServletRequest request, HttpServletResponse response)41 throws ServletException, IOException {42 this.log("時(shí)間:"+df.format(new Date())+"執(zhí)行doGet()方法...");43 }44 45 public void init() throws ServletException {46 // Put your code here47 this.log("時(shí)間:"+df.format(new Date())+"執(zhí)行init()方法...");48 }49 50 protected void service(HttpServletRequest request, HttpServletResponse response)51 throws ServletException, IOException{52 this.log("時(shí)間:"+df.format(new Date())+"執(zhí)行service()方法...");53 super.service(request, response);54 }55 56 }
運(yùn)行結(jié)果:
4.注意事項(xiàng)
注解多少會(huì)影響服務(wù)器的啟動(dòng)速度。服務(wù)器在啟動(dòng)的時(shí)候,會(huì)遍歷Web應(yīng)用的WEB-INF/classes下的所有class文件與WEB-INF/lib下的所有jar文件,以檢查哪些類使用了注解。如果程序中沒有使用任何注解,可以在web.xml中設(shè)置<web-app>的metadatacomplete屬性為true來關(guān)掉服務(wù)器啟動(dòng)時(shí)的例行檢查。
支持注解的服務(wù)器需要支持到Servlet2.5及以上規(guī)范,所以Tomcat要6.0.X及以上版本才行。
聯(lián)系客服