国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:55日期:2023-07-25 15:06:53

傳統(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,如下的源碼截圖

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

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

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

如果想要指定多個(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ò)誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 墨脱县| 宝应县| 蒙自县| 嘉义县| 宣恩县| 济阳县| 凉城县| 连云港市| 滦平县| 汨罗市| 常州市| 东丰县| 霍山县| 郎溪县| 普格县| 太谷县| 定陶县| 宜宾市| 沐川县| 江华| 高雄县| 陕西省| 抚宁县| 大邑县| 电白县| 连江县| 隆回县| 剑川县| 宝山区| 岗巴县| 扎兰屯市| 乌审旗| 上虞市| 马尔康县| 阿荣旗| 多伦县| 牡丹江市| 竹山县| 青岛市| 综艺| 武定县|