java destroy_Java开发之@PostConstruct和@PreDestroy注解

从Java EE5规范开始,Servlet增加了两个影响Servlet生命周期的注解(Annotation):@PostConstruct和@PreConstruct。这两个注解被用来修饰一个非静态的void()方法.而且这个方法不能有抛出异常声明。

使用方式,例如:

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 @PostConstruct //方式12 public voidsomeMethod(){3 ...4 }5

6 public @PostConstruct voidsomeMethod(){ //方式27 ...8 }

48304ba5e6f9fe08f3fa1abda7d326ab.png

1.@PostConstruct说明

被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。

2.@PreDestroy说明

被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。(详见下面的程序实践)

3.程序实践

web.xml

48304ba5e6f9fe08f3fa1abda7d326ab.png

1

2

3 AnnotationServlet

4 com.servlet.AnnotationServlet

5

6

7 AnnotationServlet

8 /servlet/AnnotationServlet

9

48304ba5e6f9fe08f3fa1abda7d326ab.png

AnnotationServlet

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 packagecom.servlet;2

3 importjava.io.IOException;4 importjava.io.PrintWriter;5 importjava.sql.Time;6 importjava.text.SimpleDateFormat;7 importjava.util.Date;8

9 importjavax.annotation.PostConstruct;10 importjavax.annotation.PreDestroy;11 importjavax.servlet.ServletException;12 importjavax.servlet.http.HttpServlet;13 importjavax.servlet.http.HttpServletRequest;14 importjavax.servlet.http.HttpServletResponse;15

16 public class AnnotationServlet extendsHttpServlet {17 SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss.SSS");//设置日期格式,精确到毫秒

18

19 publicAnnotationServlet(){20 System.out.println("时间:"+df.format(new Date())+"执行构造函数...");21 }22

23 public voiddestroy() {24 this.log("时间:"+df.format(new Date())+"执行destroy()方法...");25 //super.destroy();//Just puts "destroy" string in log26 //Put your code here

27 }28

29 @PostConstruct30 public voidsomeMethod(){31 //this.log("执行@PostConstruct修饰的someMethod()方法...");//注意:这样会出错

32 System.out.println("时间:"+df.format(new Date())+"执行@PostConstruct修饰的someMethod()方法...");33 }34

35 @PreDestroy36 public voidotherMethod(){37 System.out.println("时间:"+df.format(new Date())+"执行@PreDestroy修饰的otherMethod()方法...");38 }39

40 public voiddoGet(HttpServletRequest request, HttpServletResponse response)41 throwsServletException, IOException {42 this.log("时间:"+df.format(new Date())+"执行doGet()方法...");43 }44

45 public void init() throwsServletException {46 //Put your code here

47 this.log("时间:"+df.format(new Date())+"执行init()方法...");48 }49

50 protected voidservice(HttpServletRequest request, HttpServletResponse response)51 throwsServletException, IOException{52 this.log("时间:"+df.format(new Date())+"执行service()方法...");53 super.service(request, response);54 }55

56 }

48304ba5e6f9fe08f3fa1abda7d326ab.png

运行结果:

ec8cc1cfebebd91e3c2d6b8da3941a9c.png

4.注意事项

注解多少会影响服务器的启动速度。服务器在启动的时候,会遍历Web应用的WEB-INF/classes下的所有class文件与WEB-INF/lib下的所有jar文件,以检查哪些类使用了注解。如果程序中没有使用任何注解,可以在web.xml中设置的metadatacomplete属性为true来关掉服务器启动时的例行检查。

647e22174c7af79b2e5ed6e8a3929f41.png

支持注解的服务器需要支持到Servlet2.5及以上规范,所以Tomcat要6.0.X及以上版本才行。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值