首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tomcat多模块项目的Spring启动外部配置

Tomcat多模块项目的Spring启动外部配置
EN

Stack Overflow用户
提问于 2020-09-11 09:33:14
回答 1查看 540关注 0票数 0

我有四个SpringBoot2.3.x应用程序作为WAR文件的多模块Maven项目。当每个应用程序独立运行时,我了解如何为不同的配置文件(dev、test、qa)管理属性。

现在,我将应用程序部署在外部Tomcat 9.x服务器上,并且希望为每个Spring应用程序提供外部属性文件。

属性文件应该在文件系统的Tomcat外部进行外部化,如下所示:

代码语言:javascript
复制
c:/webapp/spring-config/boot-app1-prod.properties
c:/webapp/spring-config/boot-app2-prod.properties
c:/webapp/spring-config/boot-app3-prod.properties
c:/webapp/spring-config/boot-app4-prod.properties

因此,就我的理解而言,"spring.config.location“不是一个选项,因为我只能指定每个Tomcat实例的位置和一个属性文件。

我只想将这些文件外部化为活动配置文件“prod”,因此设置如下:

代码语言:javascript
复制
spring.profiles.active=prod

问题:

实现这一目标的最佳做法是什么?目前,不是一个选项。

EN

回答 1

Stack Overflow用户

发布于 2020-10-23 04:18:23

代码语言:javascript
复制
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder springApplicationBuilder) {
    return springApplicationBuilder
            .sources(ExtpropApplication.class)
            .properties(getProperties());
}

public static void main(String[] args) {
    new SpringApplicationBuilder(ExtpropApplication.class)
    .sources(ExtpropApplication.class)
    .properties(getProperties())
    .run(args);
}

您可以使用spring.config.name添加多个文件,在读取文件之前使用配置文件条件(根据您可以添加的首选项,我没有在代码中添加这些行)。

代码语言:javascript
复制
static Properties getProperties() {
    Properties props = new Properties();
    props.put("spring.config.name","boot-app1-prod.properties,boot-app2-prod.properties,boot-app3-prod.properties,boot-app4-prod.properties");
    props.put("spring.config.location", "file://"+configPath());
    return props;
}

如果路径将读取动态和非war文件,请使用以下方法读取这些文件。

代码语言:javascript
复制
public static String configPath() {
    File file = new File("."); 
    String path=file.getAbsolutePath();
    int secondLast = path.length()-2;
    String destinationPath = path.substring(0, path.lastIndexOf("/",secondLast)+1);
    String resourcePath=destinationPath+"/webapps/";
    return resourcePath;

}

--这是用于war部署的,属性位置将是动态的,如果是静态路径,我们可以在注释本身上实现。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63844404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档