一起来学SpringBoot(五)Servlet、Filter、Listenter

web开发使用Controller基本能解决大部分的需求,但是有时候我们也需要使用Servlet,因为相对于拦截和监听来说,有时候原生的还是比较好用的,现在就来简单的在SpringBoot中使用这些特殊类吧。当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet、Filter和Servlet规范的所有监听器(如HttpSessionListener监听器)。Spring boot 的主 Servlet 为 DispatcherServlet,其默认的url-pattern为“/”。也许我们在应用中还需要定义更多的Servlet,该如何使用SpringBoot来完成呢?

代码注册

首先贴出 Servlet、Filter、Listenter

package com.maoxs.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import java.io.IOException;

@Slf4j
public class TestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
log.info("我filter初始化");
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
log.info("我filter执行");
filterChain.doFilter(servletRequest, servletResponse);
}

@Override
public void destroy() {
log.info("我filter销毁");
}
}
package com.maoxs.listener;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

@Slf4j
public class TestListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
log.info("我监听器初始化");
}

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
log.info("我监听器销毁");
}
}
package com.maoxs.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Slf4j
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.info("我进入了servlet");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.info("我进入了servlet");
}
}

然后注入几个bean分别是ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean

@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new TestServlet(), "/show/*");// ServletName默认值为首字母小写,即myServlet
}
@Bean
public FilterRegistrationBean indexFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean(new TestFilter());
registration.addUrlPatterns("/");
return registration;
}
@Bean
public ServletListenerRegistrationBean servletListenerRegistrationBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new TestListener());
return servletListenerRegistrationBean;
}

运行 并访问/show/index 查看日志

ok 完全生效。


注解自动注册

同样是上面的三个类 ,去除代码注册的bean 然后加入以下注解

然后在启动类上加入注解

运行 并访问/show/index 查看日志

依然ok


小提示

在springboot使用springmvc 的时候不需要配置DispatcherServlet的因为已经自动配置了。

但是如果想要加一些初始配置参数如何解决呢,添加以下代码即可。

@Bean
public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
registration.addUrlMappings("*.do");
registration.addUrlMappings("*.json");
return registration;
}

本博文是基于springboot2.x 如果有什么不对的请在下方留言。