我有以下項目結構春天開機多模塊servletDispatchers
-Project
|-config
| |-modules
| |-admin
| |-web
|- platform
平臺是一個包含彈簧開機啓動類項目, 平臺具有依賴於配置和配置對一切都依賴於目錄模塊 平臺也是啓動mvn spring-boot:run命令的模塊。
我想完成的事情是,模塊管理和網絡(包括Web應用程序)都有自己的測繪像
- /管理
- /網絡
下面的代碼代表管理模塊中的控制器,Web模塊還包含一個類似的控制器(即點)
@Controller
public class AdminController {
@RequestMapping("/")
public String adminController() {
return "admin";
}
}
這裏的管理模塊
@Configuration
public class Config implements EmbeddedServletContainerCustomizer {
@Autowired
protected WebApplicationContext webApplicationContext;
@Autowired
protected ServerProperties server;
@Autowired(required = false)
protected MultipartConfigElement multipartConfig;
protected DispatcherServlet createDispatcherServlet() {
AnnotationConfigEmbeddedWebApplicationContext webContext = new AnnotationConfigEmbeddedWebApplicationContext();
webContext.setParent(webApplicationContext);
webContext.scan("some.base.package");
return new DispatcherServlet(webContext);
}
protected ServletRegistrationBean createModuleDispatcher(DispatcherServlet apiModuleDispatcherServlet) {
ServletRegistrationBean registration =
new ServletRegistrationBean(apiModuleDispatcherServlet,
"/admin");
registration.setName("admin");
registration.setMultipartConfig(this.multipartConfig);
return registration;
}
@Bean(name = "adminsServletRegistrationBean")
public ServletRegistrationBean apiModuleADispatcherServletRegistration() {
return createModuleDispatcher(createDispatcherServlet());
}
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setContextPath("/admin");
}
}
類似的東西的配置,某些代碼無二的Web模塊
我曾嘗試推行一些給出答案。
- Using multiple dispatcher servlets/web contexts with spring boot
- Spring Boot (JAR) with multiple dispatcher servlets for different REST APIs with Spring Data REST
- 和很多使用Google
當我讓該組件掃描,掃描兩個模塊(除去ComponentScan濾波器)
我得到一個一個不明確的映射發現異常,說這兩個控制器方法調度到相同的路徑「/」
但是,當禁用其中一個模塊的組件掃描時,確實管理模塊會映射到/ admin。
當我禁用兩個控制器時,我看到/ web和/ admin dispatchServlets被映射。
所以我明白了這個例外,但我不明白如何解決這個問題。 對我來說,它是一個必須的,我能做到這一點每個模塊,我不想在控制器類使用
@RequestMapping("/admin")
映射它
我也試過指定的contextPath和servletPath在application.properties
所以我的問題是:什麼是最好的方法來達到我的目標,或者我試圖使用彈簧引導的東西,它不是爲了。
編輯 概念證明將是不錯
我讓他們在不同的包裝,不同的模塊甚。掃描方法也不適用於我。 –