SpringBoot注册Servlet的三种⽅法详解
本⽂栈长教你如何在 Spring Boot 注册 Servlet、Filter、Listener。
⼀、Spring Boot 注册
Spring Boot 提供了ServletRegistrationBean, FilterRegistrationBean, ServletListenerRegistrationBean三个类分别⽤来注册 Servlet, Filter, Listener,下⾯是 Servlet 的⽰例代码。
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");
}
}
@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 这些组件都需要在l中进⾏配置,3.0 之后开始不再需要l这个配置⽂件了,所有的组件都可以通过代码配置或者注解来达到⽬的。
如下图所⽰,截图⾃ Servlet 3.1。
Servlet 3.0 开始提供了这 3 个注解来代替。
@WebServlet => 代替 servlet 配置
@WebFilter => 代替 filter 配置
@WebListener => 代替 listener 配置
配置 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", 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");
}
}
配置 Filter ⽰例
/**
* @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 {
适合新手的spring bootSystem.out.println("java filter init.");
String name = InitParameter("name");
String code = InitParameter("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 配置⽅式类似,上⾯的⽰例代码⼀看就懂,这⾥不再详述。
需要注意的是,为了安全考虑,内嵌服务器不会直接执⾏ 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", 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");
}
}
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 的⽅式,⼤家灵活运⽤。以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论