本文棧長教你如何在 Spring Boot 註冊 Servlet、Filter、Listener。java
更多請在Java技術棧微信公衆號後臺回覆關鍵字:boot。web
Spring Boot 提供了 ServletRegistrationBean
, FilterRegistrationBean
, ServletListenerRegistrationBean
三個類分別用來註冊 Servlet, Filter, Listener,下面是 Servlet 的示例代碼。spring
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author Java技術棧
*/
public class RegisterServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String name = getServletConfig().getInitParameter("name");
String sex = getServletConfig().getInitParameter("sex");
resp.getOutputStream().println("name is " + name);
resp.getOutputStream().println("sex is " + sex);
}
}
@Bean
public ServletRegistrationBean registerServlet() {
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
new RegisterServlet(), "/registerServlet");
servletRegistrationBean.addInitParameter("name", "javastack");
servletRegistrationBean.addInitParameter("sex", "man");
return servletRegistrationBean;
}
複製代碼
Servlet 3.0 以前,Servlet、Filter、Listener 這些組件都須要在 web.xml
中進行配置,3.0 以後開始再也不須要 web.xml
這個配置文件了,全部的組件均可以經過代碼配置或者註解來達到目的。安全
以下圖所示,截圖自 Servlet 3.1。springboot
Servlet 3.0 開始提供了這 3 個註解來代替。bash
@WebServlet => 代替 servlet 配置服務器
@WebFilter => 代替 filter 配置微信
@WebListener => 代替 listener 配置app
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author Java技術棧
*/
@WebServlet(name = "javaServlet", urlPatterns = "/javastack.cn", asyncSupported = true,
initParams = {
@WebInitParam(name = "name", value = "javastack"),
@WebInitParam(name = "sex", value = "man") })
public class JavaServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String name = getServletConfig().getInitParameter("name");
String sex = getServletConfig().getInitParameter("sex");
resp.getOutputStream().println("name is " + name);
resp.getOutputStream().println("sex is " + sex);
}
}
複製代碼
/**
* @author Java技術棧
*/
@WebFilter(filterName = "javaFilter", urlPatterns = "/*", initParams = {
@WebInitParam(name = "name", value = "javastack"),
@WebInitParam(name = "code", value = "123456") })
public class JavaFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("java filter init.");
String name = filterConfig.getInitParameter("name");
String code = filterConfig.getInitParameter("code");
System.out.println("name is " + name);
System.out.println("code is " + code);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("java filter processing.");
chain.doFilter(request, response);
}
@Override
public void destroy() {
System.out.println("java filter destroy.");
}
}
複製代碼
Listener 配置方式相似,上面的示例代碼一看就懂,這裏再也不詳述。async
須要注意的是,爲了安全考慮,內嵌服務器不會直接執行 Servlet 3.0 裏面的 javax.servlet.ServletContainerInitializer
接口,或者 Spring 中的 org.springframework.web.WebApplicationInitializer
接口,不然會致使終止 Spring Boot 應用。
因此,若是使用的是 Spring Boot 內嵌服務器,須要在配置類上面添加額外的 @ServletComponentScan
註解來開啓 Servlet 組件掃描功能,若是使用的是獨立的服務器,則不須要添加,會使用服務器內部的自動發現機制。
若是你想在 Spring Boot 中完成 Servlet、Filter、Listener 的初始化操做,你須要在 Spring 中實現下面這個接口,並註冊爲一個 bean。
org.springframework.boot.web.servlet.ServletContextInitializer
ServletContext 提供了幾個動態註冊的方法,以下所示。
如下爲動態添加 Servlet 示例代碼。
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author Java技術棧
*/
@WebServlet(name = "javaServlet", urlPatterns = "/javastack.cn", asyncSupported = true,
initParams = {
@WebInitParam(name = "name", value = "javastack"),
@WebInitParam(name = "sex", value = "man") })
public class JavaServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String name = getServletConfig().getInitParameter("name");
String sex = getServletConfig().getInitParameter("sex");
resp.getOutputStream().println("name is " + name);
resp.getOutputStream().println("sex is " + sex);
}
}
import cn.javastack.springbootbestpractice.servlet.InitServlet;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.stereotype.Component;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
/**
* @author Java技術棧
*/
@Component
public class ServletConfig implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) {
ServletRegistration initServlet = servletContext
.addServlet("initServlet", InitServlet.class);
initServlet.addMapping("/initServlet");
initServlet.setInitParameter("name", "javastack");
initServlet.setInitParameter("sex", "man");
}
}
複製代碼
本文介紹了在 Spring Boot 下的 3 種註冊 Servlet、Filter、Listener 的方式,你們靈活運用。
看完有收穫,點贊轉發一下吧!
好了,今天的分享就到這裏,更多 Spring Boot 文章正在撰寫中,關注Java技術棧微信公衆號獲取第一時間推送。在公衆號後臺回覆:boot,還能獲取棧長整理的往期 Spring Boot 教程,都是實戰乾貨,如下僅爲部分預覽。