day11-SpringBoot中注入Servlet&Filter&Listener

SpringBoot中注入Servlet&Filter&Listener

1.基本介绍

文档:SpringBoot中注入Servlet&Filter&Listener

  1. 考虑到实际开发业务非常复杂和兼容问题,SpringBoot支持将Servlet、Filter、Listener注入spring容器中,成为Spring Bean
  2. 也就是说,SpringBoot开放了和原生WEB组件(Servlet、Filter、Listener)的兼容
  3. SpringBoot注入Servlet、Filter、Listener,有两种方式:
    • 通过注解方式注入
    • 使用RegistrationBean方式注入

2.通过注解方式注入

2.1@WebServlet

属性名 对应标签 描述
name <servlet-name> 指定 Servlet 的 name 属性。 如果没有显式指定,则取值为该 Servlet 的完全限定名,即包名+类名
value <url-pattern> 该属性等价于 urlPatterns 属性,两者不能同时指定。 如果同时指定,通常是忽略 value 的取值
urlPatterns <url-pattern> 指定一组 Servlet 的 URL 匹配模式
loadOnStartup <load-on-startup> 指定 Servlet 的加载顺序
initParams <init-param> 指定一组 Servlet 初始化参数
asyncSupported <async-supported> 声明 Servlet 是否支持异步操作模式
description <description> 指定该 Servlet 的描述信息
displayName <display-name> 指定该 Servlet 的显示名

例子--使用@WebServlet注入Servlet

(1)MyServlet.java

  1. 通过继承HttpServlet来开发原生的Servlet

  2. 使用@WebServlet,表示将其标识的对象注入到Spring容器中

  3. urlPatterns = {"servlet01","servlet02"} 对此servlet配置了映射路径

  4. 对于开发的原生的Servlet,需要使用@ServletComponentScan在SpringBoot主程序中,指定要扫描的原生Servlet,这样该Servlet才能注入容器

package com.li.thymeleaf.servlet;

import javax.servlet.ServletException;
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 李
 * @version 1.0
 */
@WebServlet(urlPatterns = {"/servlet01", "/servlet02"})
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello,MyServlet!");
    }
}

(2)Application.java主程序

package com.li.thymeleaf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

/**
 * @author 李
 * @version 1.0
 */
//指定扫描Servlet
@ServletComponentScan(basePackages = "com.li.thymeleaf")
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

(3)浏览器访问地址:http://localhost:8080/servlet01获者 http://localhost:8080/servlet02,返回如下:

image-20230323182004415

注意:注入的Servlet不会被SpringBoot的拦截器拦截(因为原生Servlet和前端控制器DispatcherServlet是统一级别的,而拦截器在DispatcherServlet中)

image-20230322173641992

2.2@WebFilter

属性名 说 明
description 该过滤器的描述信息,等价于 <description>标签。
displayName 该过滤器的显示名,通常配合工具使用,等价于 <display-name> 标签
initParams 指定一组过滤器初始化参数,等价于 <init-param> 标签。
filterName 指定过滤器的 name 属性,等价于 <filter-name>
servletNames 指定过滤器将应用于哪些 Servlet。取值是 @WebServlet 中的 name 属性的取值,或者是 web.xml 中 <servlet-name> 的取值
value/urlPatterns 过滤器的 URL 匹配模式,等价于<url-pattern>标签
dispatcherTypes 指定过滤器的转发模式。具体取值包括: ASYNC、ERROR、FORWARD、INCLUDE、REQUEST。
asyncSupported 声明过滤器是否支持异步操作模式, 等价于<async-supported>标签

例子--使用@WebFilter注入Filter

  1. @WebFilter标识一个过滤器,并注入spring容器

  2. urlPatterns = {"/css/*", "/images/*"}表示请求/css/目录或者/images/目录下的资源时,请求会经过这个过滤器

  3. 需要在主程序中,指定要扫描的Filter,这样该Filter才能注入容器

package com.li.thymeleaf.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 李
 * @version 1.0
 * 开发Filter并注入spring容器
 */
@Slf4j
@WebFilter(urlPatterns = {"/css/*", "/images/*"})
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("MyFilter的init()方法被执行...");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.info("MyFilter的doFilter()方法被执行...");
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        log.info("过滤器处理的uri={}", httpServletRequest.getRequestURI());
        chain.doFilter(request, response);//放行
    }

    @Override
    public void destroy() {
        log.info("MyFilter的destroy()方法被执行...");
    }
}

(2)在主程序中配置扫描该过滤器(略)

(3)在浏览器访问地址:http://localhost:8080/images/login.jpg,后台输出:

2023-03-23 18:59:36.685  INFO 39228 --- [nio-8080-exec-6] com.li.thymeleaf.filter.MyFilter         : MyFilter的doFilter()方法被执行...
2023-03-23 18:59:36.685  INFO 39228 --- [nio-8080-exec-6] com.li.thymeleaf.filter.MyFilter         : 过滤器处理的uri=/images/login.jpg

有时候后台没有输出,可能是浏览器缓存问题

2.3@WebListener

(1)MyListener.java

package com.li.thymeleaf.listener;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author 李
 * @version 1.0
 */
@Slf4j
@WebListener
public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //可以加入项目初始化相关的业务
        log.info("MyListener-contextInitialized()-项目初始化OK~");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //可以加入业务
        log.info("MyListener-contextDestroyed()-项目初销毁...");
    }
}

(2)在主程序 Application.java配置扫描该监听器

package com.li.thymeleaf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author 李
 * @version 1.0
 */
//指定扫描监听器
@ServletComponentScan(basePackages = "com.li.thymeleaf")
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext ioc =
                SpringApplication.run(Application.class, args);
        //监听器的contextDestroyed()方法在容器销毁时触发
        ioc.stop();
    }
}

(3)启动项目,控制台输出:

image-20230323191501341

3.使用RegistrationBean方式注入

RegistrationConfig.java:

package com.li.thymeleaf.config;

import com.li.thymeleaf.filter.MyFilter;
import com.li.thymeleaf.listener.MyListener;
import com.li.thymeleaf.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * @author 李
 * @version 1.0
 * RegistrationConfig是一个配置类,
 * 默认为单实例模式 proxyBeanMethods=true
 */
@Configuration
public class RegistrationConfig {
    //使用RegistrationBean方式注入Servlet
    @Bean
    public ServletRegistrationBean servlet_() {
        MyServlet myServlet = new MyServlet();
        //将myServlet关联到ServletRegistrationBean对象
        //可以指定多个映射url
        return new ServletRegistrationBean(myServlet, "/servlet01", "/servlet02");
    }

    //使用RegistrationBean方式注入Filter
    @Bean
    public FilterRegistrationBean filter_() {
        MyFilter myFilter = new MyFilter();//创建原生的Filter对象
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
        //设置filter的urlPattern
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/css/*", "/images/*"));
        return filterRegistrationBean;
    }

    //使用RegistrationBean方式注入Listener
    @Bean
    public ServletListenerRegistrationBean listener_() {
        MyListener myListener = new MyListener();//创建原生的Listener对象
        return new ServletListenerRegistrationBean(myListener);
    }
}

使用RegistrationBean的方式注入,不必在主程序Application.java中配置扫描

运行程序,可以看到三个组件都被注入到容器中:

image-20230323201853568

4.注意事项和细节

4.1请求自定义Servlet时,为什么不会到达拦截器?

原因分析:

注入的Servlet会存在Spring容器,DispatcherServlet也存在Spring容器。当多个Servlet都能处理到同一层路径时,存在精确优先原则/最长前缀匹配原则:**精准匹配 > 目录匹配 > 扩展名匹配 > /* > / **

如下图:当浏览器请求路径为/servlet01 时,MyServlet的映射路径对与浏览器请求来说是精准匹配,因此此时MyServlet的映射路径优先级高于前端控制器的 /,请求路径会走tomcat流程,不会到达前端控制器,也就不会执行拦截器。

image-20230323204226262

当然,在SpringBoot中,去调用@Controller目标方法,仍是按照DispatcherServlet分发匹配的机制

4.2DispatcherServlet在SpringBoot如何进行配置和注入

DispatcherServletAutoConfiguration 完成对 DispatcherServlet 的自动配置。

DispatcherServletAutoConfiguration 类,有一个内部类:

@Configuration(proxyBeanMethods = false)
@Conditional(DefaultDispatcherServletCondition.class)
@ConditionalOnClass(ServletRegistration.class)
@EnableConfigurationProperties(WebMvcProperties.class)
protected static class DispatcherServletConfiguration {

    @Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
    //创建了DispatcherServlet对象,并进行一系列设置并返回。
    public DispatcherServlet dispatcherServlet(WebMvcProperties webMvcProperties) {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();
        dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
        dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
        dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
        dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
        dispatcherServlet.setEnableLoggingRequestDetails(webMvcProperties.isLogRequestDetails());
        return dispatcherServlet;
    }

    @Bean
    @ConditionalOnBean(MultipartResolver.class)
    @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
    public MultipartResolver multipartResolver(MultipartResolver resolver) {
        // Detect if the user has created a MultipartResolver but named it incorrectly
        return resolver;
    }

}

然后通过如下方法,创建DispatcherServletRegistrationBean对象,并将创建的DispatcherServlet对象关联到这个DispatcherServletRegistrationBean对象中,将DispatcherServletRegistrationBean对象通过@Bean注入到容器中。

@Configuration(proxyBeanMethods = false)
@Conditional(DispatcherServletRegistrationCondition.class)
@ConditionalOnClass(ServletRegistration.class)
@EnableConfigurationProperties(WebMvcProperties.class)
@Import(DispatcherServletConfiguration.class)
protected static class DispatcherServletRegistrationConfiguration {

   @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
   @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
   public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet,
         WebMvcProperties webMvcProperties, ObjectProvider<MultipartConfigElement> multipartConfig) {
      DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
            webMvcProperties.getServlet().getPath());//设置路径 /
      registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
      registration.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup());
      multipartConfig.ifAvailable(registration::setMultipartConfig);
      return registration;
   }

}
image-20230323212404193
posted @ 2023-03-23 21:29  一刀一个小西瓜  阅读(232)  评论(0编辑  收藏  举报