我前一段时间找到了解决方案,但忘了在这里分享,因此感谢Jan提醒我。
我通过在具有不同配置的新Web应用程序上下文( RepositoryRestMvcConfiguration )和一个公共父级(即Spring
Boot应用程序的根应用程序上下文)中创建和注册多个调度程序servlet来解决了该问题。为了根据类路径中包含的不同jar自动启用API模块,我模拟了Spring
Boot或多或少的功能。
该项目分为几个gradle模块。像这样:
- 项目服务器
- 项目API自动配置
- 项目模块API
- 项目模块b-api
- …
- 项目模块n-api
模块 项目服务器 是主要的模块。它声明了对 project-api-autoconfigure 的依赖,同时排除了对 project-api-
autoconfigure 对 project-module-?-api模块 的传递依赖。
在 project-server.gradle 内部:
dependencies { compile (project(':project-api-autoconfigure')) { exclude module: 'project-module-a-api' exclude module: 'project-module-b-api' ... } ...}project-api-autoconfigure 依赖于所有API模块,因此依赖项在 project-api-
autoconfigure.gradle 上看起来像这样:
dependencies { compile project(':project-module-a-api') compile project(':project-module-b-api') ...}__我在 project-api-autoconfigure 中为每个API模块创建具有各自Web应用程序上下文的调度程序Servlet
Bean,但是此配置取决于每个API模块jar中每个API模块的配置类。
我创建了一个抽象类,每个自动配置类都从该类继承:
public abstract class AbstractApiModuleAutoConfiguration<T> { @Autowired protected ApplicationContext applicationContext; @Autowired protected ServerProperties server; @Autowired(required = false) protected MultipartConfigElement multipartConfig; @Value("${project.rest.base-api-path}") protected String baseApiPath; protected DispatcherServlet createApiModuleDispatcherServlet() { AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext(); webContext.setParent(applicationContext); webContext.register(getApiModuleConfigurationClass()); return new DispatcherServlet(webContext); } protected ServletRegistrationBean createApiModuleDispatcherServletRegistration(DispatcherServlet apiModuleDispatcherServlet) { ServletRegistrationBean registration = new ServletRegistrationBean( apiModuleDispatcherServlet, this.server.getServletMapping() + baseApiPath + "/" + getApiModulePath() + "*' exclude 'profiles'}dependencies { compile (project(':project-api-autoconfigure')) { exclude module: 'project-module-a-api' exclude module: 'project-module-b-api' ... } ... }...def loadProfile() { def profile = hasProperty('profile') ? "${profile}" : "dev" println "Profile: " + profile apply from: "profiles/" + profile + ".gradle"}或多或少。希望对您有帮助。
干杯。



