亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring boot 實(shí)現(xiàn)配置多個DispatcherServlet最簡單方式

 更新時間:2021年01月22日 10:23:46   作者:小威架構(gòu)  
這篇文章主要介紹了spring boot 實(shí)現(xiàn)配置多個DispatcherServlet最簡單方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

傳統(tǒng)的web項(xiàng)目,只需要在web.xml里配置多個即可,并且支持多個url-pattern

在spring boot中,我們默認(rèn)無需配置,系統(tǒng)會自動裝配一個,感興趣的可以看下源碼

org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration

里面有個 DispatcherServletRegistrationBean,關(guān)鍵是這里只能指定一個path,如下的源碼截圖

如果想要指定多個,我們只能自己寫DispatcherServletRegistrationBean這個Bean了,那么系統(tǒng)就不會實(shí)例化內(nèi)置的那個了,如下代碼

@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注解,否則啟動會有報(bào)錯。

如果我們系統(tǒng)有一個接口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;
 }

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評論