SpringBoot中註入Servlet&Filter&Listener 1.基本介紹 文檔:SpringBoot中註入Servlet&Filter&Listener 考慮到實際開發業務非常複雜和相容問題,SpringBoot支持將Servlet、Filter、Listener註入spring容器中 ...
SpringBoot中註入Servlet&Filter&Listener
1.基本介紹
- 考慮到實際開發業務非常複雜和相容問題,SpringBoot支持將Servlet、Filter、Listener註入spring容器中,成為Spring Bean
- 也就是說,SpringBoot開放了和原生WEB組件(Servlet、Filter、Listener)的相容
- 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
-
通過繼承HttpServlet來開發原生的Servlet
-
使用@WebServlet,表示將其標識的對象註入到Spring容器中
-
urlPatterns = {"servlet01","servlet02"} 對此servlet配置了映射路徑
-
對於開發的原生的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
,返回如下:
註意:註入的Servlet不會被SpringBoot的攔截器攔截(因為原生Servlet和前端控制器DispatcherServlet是統一級別的,而攔截器在DispatcherServlet中)
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
-
@WebFilter標識一個過濾器,並註入spring容器
-
urlPatterns = {"/css/*", "/images/*"}
表示請求/css/目錄或者/images/目錄下的資源時,請求會經過這個過濾器 -
需要在主程式中,指定要掃描的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)啟動項目,控制台輸出:
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中配置掃描
運行程式,可以看到三個組件都被註入到容器中:
4.註意事項和細節
4.1請求自定義Servlet時,為什麼不會到達攔截器?
原因分析:
註入的Servlet會存在Spring容器,DispatcherServlet也存在Spring容器。當多個Servlet都能處理到同一層路徑時,存在精確優先原則/最長首碼匹配原則:**精準匹配 > 目錄匹配 > 擴展名匹配 > /* > / **
如下圖:當瀏覽器請求路徑為/servlet01
時,MyServlet的映射路徑對與瀏覽器請求來說是精準匹配,因此此時MyServlet的映射路徑優先順序高於前端控制器的 /
,請求路徑會走tomcat流程,不會到達前端控制器,也就不會執行攔截器。
當然,在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;
}
}