如果在我们的 Spring Boot 项目使用到了 Servlet、Filter、Listener,可以使用如下三种方式配置。
自定义 Servlet、Filter、Listener
/**
* 自定义 servlet
*/
public class CustomServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("servlet get method");
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("servlet post method");
response.getWriter().write("hello world");
}
}
/**
* 自定义 filter
*/
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("init filter");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("do filter");
chain.doFilter(request, response);
}
@Override
public void destroy() {
System.out.println("destroy filter");
}
}
/**
* 自定义 listener
*/
public class CustomListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("contextInitialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("contextDestroyed");
}
}
方式一:注册 Bean
创建自定义的 Servlet、Filter、Listener 类,然后通过在 Spring Boot 入口启动类中注册 ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 的方式使用。
@SpringBootApplication
public class DemoApplication {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new CustomServlet(), "/test");
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
return new FilterRegistrationBean(new CustomFilter(), servletRegistrationBean());
}
@Bean
public ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean() {
return new ServletListenerRegistrationBean<CustomListener>(new CustomListener());
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
方式二:实现 ServletContextInitializer 接口
通过实现 ServletContextInitializer 接口,实现其中的 onStartup 方法。
@SpringBootApplication
public class DemoApplication implements ServletContextInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addServlet("customServlet", new CustomServlet()).addMapping("/test");
servletContext.addFilter("customFilter", new CustomFilter())
.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "customServlet");
servletContext.addListener(new CustomListener());
}
}
方法三:通过注解
在 SpringBootApplication 上使用 @ServletComponentScan 注解后,直接通过@WebServlet、@WebFilter、@WebListener 注解自动注册。
@SpringBootApplication
@ServletComponentScan
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
/**
* 自定义 servlet
*/
@WebServlet(name = "customServlet", urlPatterns = "/test")
public class CustomServlet extends HttpServlet {
/**
* 自定义 filter
*/
@WebFilter(filterName = "customFilter", urlPatterns = "/*")
public class CustomFilter implements Filter {
/**
* 自定义 listener
*/
@WebListener
public class CustomListener implements ServletContextListener {