Servlet注解之@PostConstruct和@PreDestroy
今天接触到两个注解@PostConstruct和@PreDestroy,这是两个影响Servlet生命周期的注解,从JavaEE5规范开始,这两个注解被用来修饰一个非静态的void()方法,而且这个方法不能有抛出异常声明。
1.@PostConstruct说明
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的init()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
2.@PreDestroy说明
被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreConstruct修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。(详见下面的程序实践)
AnnotationServlet
package com.java.test;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AnnotationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.sss");
public AnnotationServlet() {
super();
System.out.println("时间"+dateFormat.format(new Date())+"执行构造函数。。。");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("时间"+dateFormat.format(new Date())+"执行doGET()方法。。。");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
@PostConstruct
public void someMethod() {
System.out.println("时间"+dateFormat.format(new Date())+"执行@PostConstruct修饰的someMethod()方法...");
}
@PreDestroy
public void otherMethod() {
System.out.println("时间"+dateFormat.format(new Date())+"执行@PreDestroy修饰的otherMethod()方法...");
}
public void destroy() {
System.out.println("时间"+dateFormat.format(new Date())+"执行destroy()方法。。。");
}
public void init() throws ServletException {
System.out.println("时间"+dateFormat.format(new Date())+"执行init()方法。。。");
super.init();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("时间"+dateFormat.format(new Date())+"执行service()方法。。。");
super.service(request, response);
}
}
web.xml
<servlet>
<servlet-name>AnnotationServlet</servlet-name>
<servlet-class>com.java.test.AnnotationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AnnotationServlet</servlet-name>
<url-pattern>/servlet/AnnotationServlet</url-pattern>
</servlet-mapping>
3.运行结果
时间10:24:49.049执行构造函数。。。
时间10:24:49.049执行@PostConstruct修饰的someMethod()方法…
时间10:24:49.049执行init()方法。。。
时间10:24:49.049执行service()方法。。。
时间10:24:49.049执行doGET()方法。。。
十月 17, 2017 10:24:54 上午 org.apache.catalina.core.StandardServer await
信息: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
十月 17, 2017 10:24:54 上午 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler [“http-nio-8080”]
十月 17, 2017 10:24:55 上午 org.apache.catalina.core.StandardService stopInternal
信息: Stopping service Catalina
十月 17, 2017 10:24:55 上午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextDestroyed()
十月 17, 2017 10:24:55 上午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextDestroyed()
时间10:24:55.055执行destroy()方法。。。
时间10:24:55.055执行@PreDestroy修饰的otherMethod()方法…
十月 17, 2017 10:24:55 上午 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler [“http-nio-8080”]
十月 17, 2017 10:24:55 上午 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler [“http-nio-8080”]