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 ...
随机推荐
- Java架构师/高并发/高可用/高扩展/性能优化/框架源码分析实战
https://ke.qq.com/course/401944?taid=3389721334391320
- [CSP-S模拟测试]:X国的军队(贪心)
题目描述 $X$国和$Y$国开战了! 作为$X$国的军事参谋,你了解到事态的严峻性.为了更好地应付敌人,你收集到了$Y$国城市中$n$个据点的信息,你打算攻破这$n$个据点! 每个据点$i$的信息由火 ...
- laravel的使用
1.先下载composer.phar 下载地址:https://getcomposer.org/download/ 把composer.phar拷贝到自己的项目目录中,执行以下代码: php comp ...
- ANTLR4 实验总结
问题总结: 1.不同类型编程语言之间转换的意义. 仅仅的字符替换?结构 2.编程语言转换的困难. 优先级:++运算符 不同语言有各自的优越性(Scala转C遇到困难) 编程思想冲突. 3.单纯应用在数 ...
- JS-格式化json
一 使用原生 JSON.stringify 实现 <textarea name="" id="myTA" cols="30" rows ...
- leetcode 235. 二叉搜索树的最近公共祖先(c++)
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x ...
- mybatis注解开发实体类属性和数据库字段不对应问题
/** * 查询所有用户 * @return */ @Select("select * from user") @Results(id="userMap",va ...
- Manacher(输出最长回文串及下标)
http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit: 3000/1000 MS (Java/Others ...
- windows8.1安装python
python3.8安装后缺少runtime.dll文件,试验了各种方法都不可行,最后安装了Anaconda3,这是一个python配置环境,但是好像Anaconda3只能兼容3.7,python3.8 ...
- python学习第二十五天函数位置参数和关键词参数
函数位置参数顾名思义就是按位置排序,按位置对应参数,位置一一对应,函数的关键词参数是不按照顺序来的,可以指定的参数传值.但是注意的是,位置参数必须在关键词参数之前. 1,函数位置参数 def good ...