时间:2021-05-19
传统的web项目,只需要在web.xml里配置多个即可,并且支持多个url-pattern
在spring boot中,我们默认无需配置,系统会自动装配一个,感兴趣的可以看下源码
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration里面有个 DispatcherServletRegistrationBean,关键是这里只能指定一个path,如下的源码截图
如果想要指定多个,我们只能自己写DispatcherServletRegistrationBean这个Bean了,那么系统就不会实例化内置的那个了,如下代码
@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;}这样我们参考底层源码,我们做了三个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.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; }以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Spring集成JSF的最简单方式是使用DelegatingVariableResolver类在faces-config.xml中配置,示例如下:org.spr
一)spring-boot-starter命名规则自动配置模块命名规则:xxx-spring-boot,如:aspectlog-spring-boot启动器命名
了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以通过注解自定义配置文
IntellijIDEA开发工具,基于Maven框架的SpringBoot简单示例演示启动。Maven工程pom.xml配置,主要引入spring-boot-s
SpringMVC之DispatcherServlet使用SpringMVC,配置DispatcherServlet是第一步。DispatcherServlet