免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Java開發(fā)之@PostConstruct和@PreConstruct注解

      從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及以上版本才行。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Servlet3.0
注解@PostConstruct與@PreDestroy講解及實(shí)例
Spring MVC 4.2 增加 CORS 支持
@PostConstruct 和 @PreDestroy 使用
實(shí)現(xiàn)在項(xiàng)目啟動(dòng)后或是生成對(duì)象后完成某些執(zhí)行的功能實(shí)現(xiàn)CommandLineRunner接口和注解@PostConstruct
Apache Shiro實(shí)現(xiàn)單點(diǎn)登錄SSO | 沐風(fēng)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服