springboot实现配置多个DispatcherServlet最简单⽅式
传统的web项⽬,只需要在l⾥配置多个即可,并且⽀持多个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(
Servlet().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(
Servlet().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(
Servlet().getLoadOnStartup());
实例化bean的三种方式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.ller");
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;
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。如有错误或未考虑完全的地⽅,望不吝赐教。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论