栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Boot(JAR)具有多个调度程序servlet,用于带有Spring Data REST的不同REST API

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot(JAR)具有多个调度程序servlet,用于带有Spring Data REST的不同REST API

我前一段时间找到了解决方案,但忘了在这里分享,因此感谢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"}

或多或少。希望对您有帮助。

干杯。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/615525.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号