zoukankan      html  css  js  c++  java
  • java的@PostConstruct注解

    转自:file:///E:/downloads/api/index.html

    https://www.cnblogs.com/landiljy/p/5764515.html

    javax.annotation 
    注释类型 PostConstruct


    @Documented
    @Retention(value=RUNTIME)
    @Target(value=METHOD)
    public @interface PostConstruct

    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 <!-- @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");//设置日期格式,精确到毫秒
    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中设置<web-app>的metadatacomplete属性为true来关掉服务器启动时的例行检查。

         

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

  • 相关阅读:
    转载 jenkins执行selenium 测试 浏览器不显示解决方法
    jmeter用beanshell调用自己写的jar进行MD5加密
    深入理解Spring MVC 思想
    springmvc和json整合配置方法
    getFragmentManager和getChildFragmentManager
    android
    HTTP 请求头与请求体
    Android中ImnageView 中如何让图片拉伸显示?
    ExecutorService 的理解与使用
    SpringMVC源码分析系列
  • 原文地址:https://www.cnblogs.com/YuyuanNo1/p/8184003.html
Copyright © 2011-2022 走看看