1、SpringBoot配置自定义监听器

实质上是在servlet3.0+的容器中,注册一个Servlet。

功能:监听对应的请求路径url-api

@Slf4j
@Configuration
public class SpringBootAutoConfigure
{
// 配置监听器
@Bean("xxxListener")
public ServletRegistrationBean<XxxServlet> createtListener()
{
log.info("监听请求的路径 /url-api...");
ServletRegistrationBean<XxxServlet> bean = new ServletRegistrationBean<>(
new XxxServlet());
//添加参数至LinkedHashMap
bean.addInitParameter("aaaController", "aaaController");
bean.addInitParameter("bbbController", "bbbController");
bean.addUrlMappings("/url-api");
return bean;
}
}
// 设置参数的方法
public void setInitParameters(Map<String, String> initParameters) {
Assert.notNull(initParameters, "InitParameters must not be null");
this.initParameters = new LinkedHashMap<>(initParameters);
}

2、如何注册Servlet

首先看看类和接口之间的依赖模型:

增加Servlet, ServletRegistrationBean使用泛型限定这个类是Servlet,通过构造函数参数为Servlet的对象,
注册在Servlet一个servlet3.0+的容器中。

public ServletRegistrationBean(T servlet, String... urlMappings) {
this(servlet, true, urlMappings);
}

2.1 ServletRegistrationBean

Servlet的设置和获取,MapperURL的设置添加等等

public class ServletRegistrationBean<T extends Servlet> extends DynamicRegistrationBean<ServletRegistration.Dynamic> {

private static final String[] DEFAULT_MAPPINGS = { "/*" };
private T servlet;
private Set<String> urlMappings = new LinkedHashSet<>();
private boolean alwaysMapUrl = true;
private int loadOnStartup = -1;
private MultipartConfigElement multipartConfig;

2.2 设置参数

public abstract class DynamicRegistrationBean<D extends Registration.Dynamic> extends RegistrationBean {
private static final Log logger = LogFactory.getLog(RegistrationBean.class);
private String name;
private boolean asyncSupported = true;
private Map<String, String> initParameters = new LinkedHashMap<>();

2.3 RegistrationBean

可以设置Servlet的启动顺序,实现了ServletContextInitializer的类将会被SpringServletContainerInitializer监测到

public abstract class RegistrationBean implements ServletContextInitializer, Ordered {

private static final Log logger = LogFactory.getLog(RegistrationBean.class);
private int order = Ordered.LOWEST_PRECEDENCE;
private boolean enabled = true;

2.4 接口ServletContextInitializer和Ordered

@FunctionalInterface
public interface ServletContextInitializer { public interface Ordered { /**
* Useful constant for the highest precedence value.
* @see java.lang.Integer#MIN_VALUE
*/
int HIGHEST_PRECEDENCE = Integer.MIN_VALUE; /**
* Useful constant for the lowest precedence value.
* @see java.lang.Integer#MAX_VALUE
*/
int LOWEST_PRECEDENCE = Integer.MAX_VALUE;

3、常见的初始化接口

3.1 ServletContainerInitializer

org.springframework.web.SpringServletContainerInitializer is an implementation of javax.servlet.ServletContainerInitializer.

与传统基于web.xml的配置方式不同,Servlet3.0设计了ServletContainerInitializer,ServletContainerInitializer
支持通过Spring的WebApplicationInitializer SPI编写基于代码的Servlet容器配置。

3.2  WebApplicationInitializer接口几种配置ServletContext的方式

1 传统的web.xml,代码示例

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

2 实现这个接口WebApplicationInitializer ,代码示例。主要复写onStartup方法。

public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml"); ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}

3 基于代码方式的全配置,代码示例。

public class MyWebAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class); // Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class); // Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}

3.3 org.springframework.boot.web.support.SpringBootServletInitializer (class)

这是一个通过传统的WAR包部署方式运行SpringApplication的WebApplicationInitializer实现。它可以将应用容器中的Servlet、
Filter和ServletContextInitializer相关的bean绑定到服务器(ServletContainer)。

3.4 org.springframework.boot.web.servlet.ServletContextInitializer (interface)

不同于WebApplicationInitializer,该接口的实现类不会被SpringServletContainerInitializer识别因此不会被Servlet容器自动执行。
ServletContextInitializers主要被Spring管理而不是Servlet容器。

SpringBoot动态注册Servlet的更多相关文章

  1. 使用ServletContainerInitializer动态注册组件

    1.背景 在web容器(例如tomcat)启动时为提供给第三方组件机会做一些初始化的工作,例如注册servlet或者filtes等.对此servlet规范提供了ServletContainerInit ...

  2. Servlet - Upload、Download、Async、动态注册

    Servlet 标签 : Java与Web Upload-上传 随着3.0版本的发布,文件上传终于成为Servlet规范的一项内置特性,不再依赖于像Commons FileUpload之类组件,因此在 ...

  3. SpringBoot注册Servlet、Filter、Listener

    SpringBoot默认是以jar包的方式启动嵌入式的Servlet容易来启动SpringBoot的Web应用,没有web.xml文件 因此我们可以使用以下方式来注册Servlet.Filter.Li ...

  4. SpringBoot注册Servlet/Filter/Listener

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,那么没有web.xml文件,如何配置我们的三大Web基础组件呢? 通过使用XXXRe ...

  5. Spring-Boot自动装载servlet

    Spring-Boot自动装载servlet 本人spring-boot相关博客均自己手动编写,但技术均从简书 恒宇少年 处学习,该大佬一直是我的偶像,鉴于能充分理解,所以已做笔记的方式留下这些文档, ...

  6. 18. Spring Boot 、注册Servlet三大组件Servlet、Filter、Listener

    由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,没有web.xml文件 public class MyServlet extends ...

  7. Spring-IOC BeanFactory运行时动态注册bean

    在spring运行时,动态的添加bean,dapeng框架在解析xml的字段时,使用到了动态注册,注册了一个实现了FactoryBean类! 定义一个没有被Spring管理的Controller pu ...

  8. SpringBoot27 JDK动态代理详解、获取指定的类类型、动态注册Bean、接口调用框架

    1 JDK动态代理详解 静态代理.JDK动态代理.Cglib动态代理的简单实现方式和区别请参见我的另外一篇博文. 1.1 JDK代理的基本步骤 >通过实现InvocationHandler接口来 ...

  9. Spring Boot 注册 Servlet 的三种方法,真是太有用了!

    本文栈长教你如何在 Spring Boot 注册 Servlet.Filter.Listener. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spr ...

随机推荐

  1. css让字体细长

    transform: scale(1,3); -ms-transform: scale(1,3); -webkit-transform: scale(1,3); -moz-transform: sca ...

  2. node.js ffmpeg-concat 命令行形式处理多个视频的过度效果

    ffmpeg-concat 是利用 gl-transitions 处理多个视频的过度效果.详细说明参见 https://github.com/transitive-bullshit/ffmpeg-co ...

  3. python字符串的学习计划

    python字符串有14小节内容, 计划7天学完吧(不知道能完成不) 今天依然是在禅道上写用例的一天 禅道上的用例,编写的时候比较方便 修改维护的时候,有点小麻烦(没有在Excel表中容易修改) D5 ...

  4. codeforces 1186C Vus the Cossack and Strings

    题目链接:https://codeforc.es/contest/1186/problem/C 题目大意:xxxxx(自认为讲不清.for instance) 例如:a="01100010& ...

  5. 各种CNN模型

    Resnet: model_urls = { 'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth', 'res ...

  6. 四、Zabbix-zabbix agent部署

    1.添加zabbix安装源 rpm -i http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch ...

  7. 为企业服务器配置RAID0、raid1、 raid10、raid5、raid6、等常见RAID

    RAID卡操作手册先从开机启动时如何进入管理界面开始介绍: 1)当机器开启后,显示器出现阵列卡检测信息时,会提示用户是否要进入管理界面对阵列卡进行操作,此时按下Ctrl + H 即可,如下图 2)按下 ...

  8. Docker最详细入门教程

    Docker原理.详细入门教程 https://blog.csdn.net/deng624796905/article/details/86493330 阮一峰Docker入门讲解 http://ww ...

  9. kafka 安装教程

    安装详述: https://www.jianshu.com/p/596f107e901a 3.0:运行:cd 到: D:\Installed_software\Professional\kafka_2 ...

  10. JExcel - 学习总结(1)

    1.什么是JExcel JExcel是Java对Excel进行操作的包,可以实现创建一个Excel并写入或读取Excel的数据等操作: JExcel的主要类为: (1)Workbook:工作簿 (2) ...