spring boot 实现配置多个DispatcherServlet最简单方式
作者:小威架构 时间:2023-12-18 03:24:46
传统的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;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/zwzw1219/article/details/82287063
标签:spring,boot,DispatcherServlet
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C++智能指针读书笔记
2022-02-24 06:51:28
Android通过SOCKET下载文件的方法
2023-08-27 01:11:48
Android使用Handler实现定时器与倒计时器功能
2022-03-30 09:06:57
![](https://img.aspxhome.com/file/2023/2/137152_0s.png)
详解Struts2中json 相互引用死循环解决办法
2022-09-08 04:04:46
Java SpringBoot实现带界面的代码生成器详解
2023-09-28 11:54:14
![](https://img.aspxhome.com/file/2023/0/63000_0s.png)
一文让你搞懂如何手写一个redis分布式锁
2023-11-29 02:46:30
Android 安全加密:Https编程详解
2023-11-08 06:58:51
![](https://img.aspxhome.com/file/2023/5/139215_0s.png)
c# 在windows中操作IIS设置FTP服务器的示例
2023-07-18 06:13:01
![](https://img.aspxhome.com/file/2023/6/66116_0s.jpg)
C#访问及调用类中私有成员与方法示例代码
2022-06-02 05:30:35
![](https://img.aspxhome.com/file/2023/5/96375_0s.jpg)
android studio3.0以上如何通过navicat访问SQLite数据库文件
2022-03-26 14:28:55
![](https://img.aspxhome.com/file/2023/5/89555_0s.png)
Android运用onTouchEvent自定义滑动布局
2021-09-24 04:39:06
![](https://img.aspxhome.com/file/2023/0/97880_0s.gif)
Java NIO:浅析IO模型_动力节点Java学院整理
2022-01-17 17:07:38
![](https://img.aspxhome.com/file/2023/8/65178_0s.png)
Android Studio项目适配AndroidX(Android 9.0)的方法步骤
2022-10-30 12:01:03
![](https://img.aspxhome.com/file/2023/6/100626_0s.png)
Andriod 资源文件之存取操作
2021-11-22 09:26:52
Unity2D实现游戏回旋镖
2022-07-16 07:13:50
![](https://img.aspxhome.com/file/2023/8/66118_0s.gif)
C#日期格式强制转换方法(推荐)
2022-09-19 10:24:57
Maven profile实现不同环境的配置管理实践
2021-11-11 09:52:15
![](https://img.aspxhome.com/file/2023/5/64515_0s.png)
JavaWeb实现简单文件上传功能
2022-11-19 04:15:41
![](https://img.aspxhome.com/file/2023/1/86791_0s.jpg)
Java并发(Runnable+Thread)实现硬盘文件搜索功能
2023-05-06 04:59:22
![](https://img.aspxhome.com/file/2023/9/86699_0s.png)
详解Java中的Vector
2023-06-05 01:40:49