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,返回如下:

注意:注入的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

  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)启动项目,控制台输出:

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;
} }

day11-SpringBoot中注入Servlet&Filter&Listener的更多相关文章

  1. SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式

    在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...

  2. springboot 注入Servlet,Filter,Listener的方法

    其实就是注入 FilterRegistrationBean . ServletRegistrationBean . ServletListenerRegistrationBean 这三个类   直接上 ...

  3. Spring boot中使用servlet filter

    Spring boot中使用servlet filter liuyuhang原创,未经允许请勿转载! 在web项目中经常需要一些场景,如参数过滤防止sql注入,防止页面攻击,空参数矫正等, 也可以做成 ...

  4. ServletContextInitializer添加 servlet filter listener

    ServletContextInitializer添加 servlet filter listener https://www.cnblogs.com/pomer-huang/p/9639322.ht ...

  5. SpringBoot---注册Servlet,Filter,Listener

    1.概述 1.1.当使用  内嵌的Servlet容器(Tomcat.Jetty等)时,将Servlet,Filter,Listener  注册到Servlet容器的方法: 1.1.1.直接注册Bean ...

  6. servlet filter listener interceptor 知识点

    这篇文章主要介绍 servlet filter listener interceptor 之 知识点.博文主要从 概念,生命周期,使命介绍其区别.详情如下:   概念 生命周期 使命 servlet ...

  7. JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下)

    JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下) Reference

  8. SpringBoot中使用Servlet,Filter,Listener

    项目最近在替换之前陈旧的框架,改用SpringBoot进行重构,初接触,暂时还没有用到Servlet,Filter,Listener的地方,但在之前回顾Servlet的生命周期时,https://ww ...

  9. spring boot中使用servlet、listener和filter

    spring boot中支持使用java Web三大组件(servlet.listener和filter),但是坑比较多,主要是spring boot内嵌tomcat和独立tomcat服务器有一些细节 ...

  10. [转]web.xml中servlet ,filter ,listener ,interceptor的作用与区别

    原文链接:https://blog.csdn.net/netdevgirl/article/details/51483273 一.概念: 1.servlet:servlet是一种运行服务器端的java ...

随机推荐

  1. setAttribute动态添加

    btnDel.setAttribute('type', 'button'); //type="button" btnDel.setAttribute('value', '删除'); ...

  2. spring-security-oauth2-authorization-server

    旧依赖的移除 长久以来,使用Spring Security整合oauth2,都是使用Spring Security Oauth2这个系列的包: <dependency> <group ...

  3. Debug --> 箱线图

    箱线图主要用于反映原始数据分布的特征,还可以进行多组数据分布特征的比较. 箱形图最大的优点就是不受异常值的影响,能够准确稳定地描绘出数据的离散分布情况,同时也利于数据的清洗. 在箱图中,最上方和最下方 ...

  4. 狂神day3

    放个假,有几天没有学习了,也好久没有更新博客了 IDEA使用 在上一次的时候,我们直接建立一个工程写了一个"helloworld"的java文件,今天首先说的是先建立一个空的工程, ...

  5. React中的CSS模块

    CSS模块       使用步骤:         1.新建一个XXX.moudle.css文件         2.在组件中引入css           impor classes(变量) fro ...

  6. 使用SecureCRT通过SSH连接远程Linux设备

    Ubuntu安装和配置ssh教程 https://blog.csdn.net/future_ai/article/details/81701744 以SecureCRT为例: 把电脑和设备连接在同一个 ...

  7. Centos8 防火墙

    Centos8 防火墙 一: 防火墙端口相关操作 1:查看防火墙某个端口是否开放 firewall-cmd --query-port=3306/tcp 2:开放防火墙端口 注意:开放端口后要重启防火墙 ...

  8. C++解决海盗分金问题

    #include <iostream> #include <unordered_map> #include <map> // 假定:每个海盗都是一样的聪明,没有谁比 ...

  9. php上传文件时出现 caution: request is not finished yet

    其中的一个原因:是wamp64下的tmp文件夹中的临时文件太多,把这个文件夹的临时文件清理后就可以了.

  10. 磊磊零基础打卡算法:day20 c++dfs树的深度优先遍历

    5.24 dfs深度优先搜索: 思想比较简单,就是一条路走到底,走到最深点处再回退一步,再看有没有路可以走,没有的话再回退一步,重复此步骤: 也是人们常讲的暴搜. 主要的用法: 通常需要一个状态数组来 ...