day11-SpringBoot中注入Servlet&Filter&Listener

2023-03-24 06:06:12

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