SpringBoot---注册Servlet,Filter,Listener
1、概述
1.1、当使用 内嵌的Servlet容器(Tomcat、Jetty等)时,将Servlet,Filter,Listener 注册到Servlet容器的方法:
1.1.1、直接注册Bean
1.1.2、注册ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean 的Bean
eg:
package com.an; import com.an.servletfilterlistener.MyFilter;
import com.an.servletfilterlistener.MyListener;
import com.an.servletfilterlistener.MyServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/14 19:56
* @since:
*/
@SpringBootApplication
public class MyDemoApplication { public static void main(String[] args) {
SpringApplication.run(MyDemoApplication.class, args);
} /**
* 内嵌Servlet容器中注册Servlet、Filter、Listener---第一种方式:直接注册Bean
*/
@Bean
public MyServlet getMyServlet(){
return new MyServlet();
} @Bean
public MyFilter getMyFilter(){
return new MyFilter();
} @Bean
public MyListener getMyListener(){
return new MyListener();
} /**
* 内嵌Servlet容器中注册Servlet、Filter、Listener---第二种方式:注册ServletRegistrationBean、FilterRegistrationBean、ServletListenerRegistrationBean 的Bean
*/
public ServletRegistrationBean getServletRegistrationBean(){
return new ServletRegistrationBean(new MyServlet(),"/**");
} public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.setOrder(2);
return filterRegistrationBean;
} public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
return new ServletListenerRegistrationBean(new MyListener());
} }
SpringBoot---注册Servlet,Filter,Listener的更多相关文章
- SpringBoot注册Servlet/Filter/Listener
由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,那么没有web.xml文件,如何配置我们的三大Web基础组件呢? 通过使用XXXRe ...
- springboot 注入Servlet,Filter,Listener的方法
其实就是注入 FilterRegistrationBean . ServletRegistrationBean . ServletListenerRegistrationBean 这三个类 直接上 ...
- SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式
在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...
- ServletContextInitializer添加 servlet filter listener
ServletContextInitializer添加 servlet filter listener https://www.cnblogs.com/pomer-huang/p/9639322.ht ...
- JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下)
JavaWeb三大组件(Servlet,Filter,Listener 自己整理,初学者可以借鉴一下) Reference
- servlet filter listener interceptor 知识点
这篇文章主要介绍 servlet filter listener interceptor 之 知识点.博文主要从 概念,生命周期,使命介绍其区别.详情如下: 概念 生命周期 使命 servlet ...
- SpringBoot注册Servlet、Filter、Listener
SpringBoot默认是以jar包的方式启动嵌入式的Servlet容易来启动SpringBoot的Web应用,没有web.xml文件 因此我们可以使用以下方式来注册Servlet.Filter.Li ...
- SpringBoot整合WEB开发--(九)整合Servlet,Filter,Listener
简介: 如果需要整合第三方框架时,可能还是不得不使用Servlet,Filter,Listener,Springboot中也有提供支持. @WebServlet("/my") pu ...
- Spring Boot整合Servlet,Filter,Listener,访问静态资源
目录 Spring Boot整合Servlet(两种方式) 第一种方式(通过注解扫描方式完成Servlet组件的注册): 第二种方式(通过方法完成Servlet组件的注册) Springboot整合F ...
随机推荐
- 【2019 Multi-University Training Contest 7】
01:https://www.cnblogs.com/myx12345/p/11653845.html 02: 03: 04: 05: 06:https://www.cnblogs.com/myx12 ...
- python爬虫学习之路-遇错笔记-1
当在运行爬虫时同时开启了Fidder解析工具时(此爬虫并不是用于爬取手机端那内容,而是爬去电脑访问的网页时),访问目标站点会遇到以下错误: File "C:\Users\litao\AppD ...
- 理解CSS中position的各个值
static position的默认值,没有定位,元素在normal flow中: fixed 相对于浏览器左上角定位: relative 相对定位元素,其位置根据其在normal flow中的位置来 ...
- ModuleNotFoundError: No module named 'mysql'
问题:使用sqlalchemy初始化数据库报错,如图: 解决办法: pip install mysql-connector 安装后即可正常运行了 参照: https://blog.csdn.net/j ...
- H3C CAS 介绍 & 基本概念
目录 目录 基本概念 H3C CAS 中的虚拟机 虚拟机中的虚拟设备 虚拟 CPU 的 3 种工作模式 虚拟网卡的 3 种类型 虚拟磁盘的三种类型 虚拟机辅助工具 CAS Tools 虚拟机外的虚拟设 ...
- Python-位操作 ( &,|,~,^,<<,>>
用于提高运算速度,规避算术运算符 在位操作运算中,不应该试图考虑先得到一个数的二进制码 ,而是应该将这个数看作是一个二进制码,二进制补码与整数之间是一一对应的. 诚然Python中有内置函数bin将一 ...
- Java Web项目启动执行顺序
一. 1.启动一个WEB项目,WEB容器会先去读取它的配置文件web.xml,读取<context-param>和<listener>两个节点. 2.接着,容器创建一个Serv ...
- AtCoder ABC 140D Face Produces Unhappiness
题目链接:https://atcoder.jp/contests/abc140/tasks/abc140_d 题目大意 有一对 N 个人, 用字符串 S 表示, S[i] 如果等于 'L' 说明这个人 ...
- sql server 2008查询时报错,消息:8155,没有为'a'的列2指向任何列
解决方法1: 关掉Sql Server再打开, 重新查询 解决方法2: select T.* from( select row_number() over(order by age desc) a ...
- charles模拟弱网情况
网络主要需要注意什么场景: 弱网功能测试 无网状态测试 网络切换测试 用户体验关注 下面我们使用charles测试弱网,针对不同网络下的测试 打开charles(抓包软件)