注解类型PostConstruct版权声明

原创
小哥 3年前 (2022-10-28) 阅读数 45 #大杂烩

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 2 3 AnnotationServlet 4 com.servlet.AnnotationServlet 5 6 7 AnnotationServlet 8 /servlet/AnnotationServlet 9

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中设置的metadatacomplete属性为true在服务器启动时关闭例行检查。

支持注释的服务器需要支持Servlet2.5和上面的规格,所以Tomcat要6.0.X还有更高的。

不积小步,成不了河、成不了海。

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门