spring boot 實(shí)現(xiàn)配置多個(gè)DispatcherServlet最簡(jiǎn)單方式
傳統(tǒng)的web項(xiàng)目,只需要在web.xml里配置多個(gè)即可,并且支持多個(gè)url-pattern
在spring boot中,我們默認(rèn)無(wú)需配置,系統(tǒng)會(huì)自動(dòng)裝配一個(gè),感興趣的可以看下源碼
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration
里面有個(gè) DispatcherServletRegistrationBean,關(guān)鍵是這里只能指定一個(gè)path,如下的源碼截圖
如果想要指定多個(gè),我們只能自己寫DispatcherServletRegistrationBean這個(gè)Bean了,那么系統(tǒng)就不會(huì)實(shí)例化內(nèi)置的那個(gè)了,如下代碼
@Autowired
private WebMvcProperties webMvcProperties;@Autowiredprivate 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;}
這樣我們參考底層源碼,我們做了三個(gè)Bean,注意有一個(gè)一定要加上@Primary注解,否則啟動(dòng)會(huì)有報(bào)錯(cuò)。
如果我們系統(tǒng)有一個(gè)接口url是/api/test,那么通過(guò)/aaa/api/test或者/bbb/api/test也都可以訪問(wèn)了。
不建議的寫法、、、
@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; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. 如何使用Python的Pandas庫(kù)繪制折線圖2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)3. php中yii框架實(shí)例用法4. HTML實(shí)現(xiàn)title 屬性換行小技巧5. 父div高度不能自適應(yīng)子div高度的解決方案6. ASP替換、保存遠(yuǎn)程圖片實(shí)現(xiàn)代碼7. PHP中Session會(huì)話的使用和分析8. SSM框架整合之Spring+SpringMVC+MyBatis實(shí)踐步驟9. 三道java新手入門面試題,通往自由的道路--鎖+Volatile10. 網(wǎng)頁(yè)中img圖片使用css實(shí)現(xiàn)等比例自動(dòng)縮放不變形(代碼已測(cè)試)
