当前位置: 首页 > 知识库问答 >
问题:

Tomcat上多模块项目的Spring Boot外部配置

颛孙高义
2023-03-14

现在,我将应用程序部署在外部Tomcat9.x服务器上,并且希望每个Spring Boot应用程序都有外部属性文件。

属性文件应该外部化到文件系统上的Tomcat之外,如下所示:

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实例指定位置和一个属性文件。

spring.profiles.active=prod

共有1个答案

阚英睿
2023-03-14
@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添加多个文件,在读取文件之前使用配置文件条件(我没有在代码上添加这些行,根据您的偏好您可以添加)

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文件,则使用以下方法读取文件。

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部署和属性位置将是动态的,如果它是静态路径,我们可以在注释本身上实现。

 类似资料:
  • 我在教程两个模块的帮助下创建了一个springboot mutimodule项目--一个后端(java类),另一个forntend(angular 5应用程序)我在后端模块中包含了前端模块的依赖项。我正在创建一个jar使用maven资源插件。我也在将静态资源复制到pom.xml中构建目录的静态文件夹中。我还有一个@Controller返回“index”。当我运行jar时,我希望看到index.ht

  • 我有一个基本的SpringBoot应用程序。使用Spring初始值设定项、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行的JAR文件。是一个带有Spring Boot的多模块项目,该项目将有3个模块。这里是父模块pom。xml 这里是模块核心 这里是模块web: 从父根目录,我使用以下方法生成所有模块: 但问题是tdk-web-0.0.2-SNAPSHOT。jar不包含tdk-c

  • 父项目为demo-mybatis子模块为mybatis-dao、demo-service。demo-mybatis pom.xml如下: 4.0.0 pom mybatis-DAO Demo-service Demo-mybatis-app org.springframework.Boot spring-boot-starter-parent 2.3.2.release com.example D

  • 我有一个带有myapp-core和myapp-web模块的myapp父pom类型maven项目。myapp-core模块作为依赖项添加到MyApp-Web。 myapp-core模块中的所有类都位于根包com.myapp.core中,而myapp-web模块中的所有类都位于根包com.myapp.web中 主要的application.java也在com.myapp.web包中。由于我的核心模块根

  • 我正在学习本教程https://spring.io/guides/gs/multi-module/#scratch 在根目录中,我创建了3个目录:moduleA、moduleB和moduleC 我运行了

  • null null 的POM: 的POM:http://maven.apache.org/xsd/maven-4.0.0.xsd“>4.0.0 此项目成功地从顶部编译,但运行失败: