在普通的 WEB 应用中,Servlet、Filter、Listener 都是在 web.xml 配置文件中配置的。而在 Spring Boot 中,由于它省去 web.xml 配置文件,Servlet、Filter、Listener 的配置需要通过 Java 代码的方式来进行配置。
1. 通过 Bean 的方式配置
Spring Boot 提供了 ServletRegistrationBean、FilterRegistrationBean、
ServletListenerRegistrationBean 三种类型来分别配置应用的 Servlet、Filter、Listener。
# 在 Spring Boot 应用中配置使用 Servlet、Filter、Listener
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @Configuration public class ApplicationConfigurer { @Bean public FilterRegistrationBean registerCustomFilter() { FilterRegistrationBean filterRegBean = new FilterRegistrationBean(new CustomFilter()); filterRegBean.setUrlPatterns(Arrays.asList("/*")); return filterRegBean; } @Bean public ServletRegistrationBean registerCustomServlet() { ServletRegistrationBean servletRegBean = new ServletRegistrationBean(new CustomServlet()); servletRegBean.setUrlMappings(Arrays.asList("/customServlet")); servletRegBean.setLoadOnStartup(1); return servletRegBean; } @Bean public ServletListenerRegistrationBean<?> registerCustomListener() { return new ServletListenerRegistrationBean<>(new CustomListener()); } }
|
# 自定义的 Filter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class CustomFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("------ CustomFilter ------"); chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
|
# 自定义的 Listener
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class CustomListener implements ServletRequestListener { @Override public void requestInitialized(ServletRequestEvent event) { System.out.println("------ CustomListener ------"); } @Override public void requestDestroyed(ServletRequestEvent event) { } }
|
# 自定义的 Servlet
1 2 3 4 5 6 7 8
| public class CustomServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("------ CustomServlet ------"); } }
|
2. 通过注解的方式配置
Spring Boot 提供了 @WebServlet、@WebFilter、@WebListener 三种类型的注解来分别配置应用的 Servlet、Filter、Listener。
# Filter 注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @WebFilter(urlPatterns = "/*") public class CustomFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("------ CustomFilter Annotation ------"); chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } }
|
# Listener 注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @WebListener public class CustomListener implements ServletRequestListener { @Override public void requestInitialized(ServletRequestEvent event) { System.out.println("------ CustomListener Annotation ------"); } @Override public void requestDestroyed(ServletRequestEvent event) { } }
|
# Servlet 注解
1 2 3 4 5 6 7 8 9
| @WebServlet(urlPatterns = "/customServlet", loadOnStartup = 1) public class CustomServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("------ CustomServlet Annotation ------"); } }
|
最后,需要在应用中使用@ServletComponentScan
注解配置才有效。
1 2 3 4 5 6 7 8 9
| @SpringBootApplication @ServletComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
|
示例项目开发环境:Java-8、Maven-3、IntelliJ IDEA-2017、Spring Boot-1.5.2.RELEASE
完整示例项目链接:conf-sample scan-sample
参考文档文献链接:http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/#boot-features-embedded-container