spring boot 實現(xiàn)配置多個DispatcherServlet最簡單方式
傳統(tǒng)的web項目,只需要在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)就不會實例化內(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注解,否則啟動會有報錯。
如果我們系統(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
MyBatis學(xué)習(xí)教程(六)-調(diào)用存儲過程
這篇文章主要介紹了MyBatis學(xué)習(xí)教程(六)-調(diào)用存儲過程的相關(guān)資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧2016-05-05SpringCloud災(zāi)難性雪崩效應(yīng)處理方法之降級實現(xiàn)流程詳解
這篇文章主要介紹了SpringCloud災(zāi)難性雪崩效應(yīng)處理方法之降級,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧<BR>2022-11-11IDEA設(shè)置字體隨鼠標(biāo)滾動放大縮小的實現(xiàn)
這篇文章主要介紹了IDEA設(shè)置字體隨鼠標(biāo)滾動放大縮小的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Java基本數(shù)據(jù)類型與封裝類型詳解(int和Integer區(qū)別)
這篇文章主要介紹了Java基本數(shù)據(jù)類型與封裝類型詳解(int和Integer區(qū)別) ,需要的朋友可以參考下2017-02-02