传统的web项目,只需要在web.xml里配置多个即可,并且支持多个url-pattern

在spring boot中,我们默认无需配置,系统会自动装配一个,感兴趣的可以看下源码

org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,里面有个 DispatcherServletRegistrationBean,关键是这里只能指定一个path,如下的源码截图

如果想要指定多个,我们只能自己写DispatcherServletRegistrationBean这个Bean了,那么系统就不会实例化内置的那个了,如下代码

@Autowired
private WebMvcProperties webMvcProperties;
@Autowired
private MultipartConfigElement multipartConfig; @Bean
@Primary
public DispatcherServletRegistrationBean dispatcherServlet1(DispatcherServlet dispatcherServlet) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(
dispatcherServlet, "/*");
registration.setName("dispatcherServlet1");
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
} @Bean
public DispatcherServletRegistrationBean dispatcherServlet2(DispatcherServlet dispatcherServlet) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(
dispatcherServlet, "/aaa/*");
registration.setName("dispatcherServlet2");
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
} @Bean
public DispatcherServletRegistrationBean dispatcherServlet3(DispatcherServlet dispatcherServlet) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(
dispatcherServlet, "/bbb/*");
registration.setName("dispatcherServlet3");
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}

这样我们参考底层源码,我们做了三个Bean,注意有一个一定要加上@Primary注解,否则启动会有报错。

如果我们系统有一个接口url是/api/test,那么通过/aaa/api/test或者/bbb/api/test也都可以访问了。

不建议的写法、、、

@Bean
public ServletRegistrationBean apiDispatcherServlet(){
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.scan("com.be.edge.asset.web.api");
DispatcherServlet apiDispatcherServlet = new DispatcherServlet(applicationContext);
ServletRegistrationBean registrationBean = new ServletRegistrationBean(apiDispatcherServlet);
registrationBean.addInitParameter("throwExceptionIfNoHandlerFound", "true");
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/api/*");
registrationBean.setName("apiDispatcherServlet");
return registrationBean;
} @Bean
public ServletRegistrationBean mgmtDispatcherServlet(){
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.scan("com.be.edge.asset.web.controller");
DispatcherServlet apiDispatcherServlet = new DispatcherServlet(applicationContext);
ServletRegistrationBean registrationBean = new ServletRegistrationBean(apiDispatcherServlet);
registrationBean.setLoadOnStartup(2);
registrationBean.addInitParameter("throwExceptionIfNoHandlerFound", "true");
registrationBean.addUrlMappings("/mgmt/*");
registrationBean.setName("mngDispatcherServlet");
return registrationBean;
}

spring boot 配置多个DispatcherServlet的更多相关文章

  1. Spring Boot配置多数据源并实现Druid自动切换

    原文:https://blog.csdn.net/acquaintanceship/article/details/75350653 Spring Boot配置多数据源配置yml文件主数据源配置从数据 ...

  2. Spring Boot -- 配置切换指南

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  3. Spring Boot 配置优先级顺序

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  4. spring boot 配置注入

    spring boot配置注入有变量方式和类方式(参见:<spring boot 自定义配置属性的各种方式>),变量中又要注意静态变量的注入(参见:spring boot 给静态变量注入值 ...

  5. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

  6. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...

  7. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  8. [转] Spring Boot配置多个DataSource

    [From]  https://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000 Sprin ...

  9. Spring boot 配置异步处理执行器

    示例如下: 1. 新建Maven 项目 async-executor 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  10. Spring boot配置404、500页面

    Spring boot 配置404页面很简单,如果你访问的url没有找到就会出现spring boot 提示的页面,很明显Spring boot不用配置就可以显示404页面了. 在template下创 ...

随机推荐

  1. Spring事务的1道面试题

    每次聊起Spring事务,好像很熟悉,又好像很陌生.本篇通过一道面试题和一些实践,来拆解几个Spring事务的常见坑点. 原理 Spring事务的原理是:通过AOP切面的方式实现的,也就是通过代理模式 ...

  2. Oracle数据库安装配置详细教程汇总(含11g、12c、18c、19c、21c)

    不论你是数据库小白,还是久经沙场的技术专家,你接触和运维Oracle数据库的第一步可能都是安装配置.并且随着软硬件的升级.替换以及业务场景的变化,数据库安装也将是你常常会进行的操作之一. 这里先为大家 ...

  3. md转换成_post下直接使用的文件

    md转换成_post下直接使用的文件 package org.example; import java.io.File; import java.io.IOException; import java ...

  4. SpringBoot开启Gzip接口报文压缩

    背景 当我们一个接口响应报文比较大的时候,超过几兆甚至几十兆的情况下,减少响应体的报文大小是能有效减少响应时间的. spring boot 配置 server: compression: ## 开启服 ...

  5. 本文是第一篇在GitHub仓库中撰写的.md格式的blog文件

    正文内容: 具体内容,只是未来测试,给出福利: 模板格式: title: 博文标题 description: 博文摘要 #多个标签请使用英文逗号分隔或使用数组语法 tags: 标签1, 标签2 #多个 ...

  6. getRawType:获取数据类型,返回结果为 Number、String、Object、Array等

    function getRawType(value) { return Object.prototype.toString.call(value).slice(8, -1)}//getoRawType ...

  7. PostgreSQL加密连接SSL配置

    PostgreSQL加密连接SSL配置 环境说明 操作系统 主机名 IP 类型 说明 CentOS Linux release 7.6.1810 (Core) centos7.6 192.168.1. ...

  8. 干货分享:Air700ECQ的硬件设计,第三部分

    ​ 5. 电器特性,可靠性,射频特性 5.1. 绝对最大值 下表所示是模块数字.模拟管脚的电源供电电压电流最大耐受值. 表格 17:绝对最大值 参数 最小 最大 单位 VBAT -0.3 4.7 V ...

  9. 100 款支持 .NET 多版本的强大 WPF 控件库

    前言 推荐一款集成了超过100款控件的流行 XAML 控件库,同时提供了一系列常用的 .NET 帮助类-CookPopularUI.它可以简化开发流程,让我们能够更加专注于核心业务逻辑的实现. 让我们 ...

  10. 2023NOIP A层联测9 T3 天竺葵

    2023NOIP A层联测9 T3 天竺葵 题面及数据范围 Ps:连接为accoderOJ. 看题大概是一个最长上升子序列的带权版本,于是想到 dp. 设 \(dp[i][j]\) 为到第 \(i\) ...