【问题标题】:How to read the application.properties file from a location outside the spring boot application如何从 Spring Boot 应用程序之外的位置读取 application.properties 文件
【发布时间】:2022-01-18 14:10:14
【问题描述】:

我有一个 Spring Boot 应用程序,我想从中排除 Spring Boot 中默认创建的 application.properties 并从另一个位置读取它。但是我注意到下面显示的配置文件总是会尝试获取项目中的application.properties 文件。

Config.java 文件:

package com.eMcREY.ChatServiceProject;

import org.springframework.beans.factory.annotation.Value;

@org.springframework.context.annotation.Configuration
public class Config {
    
//  private @Value("${project.testVar:defaultValue}") String test;

//  public String getTest() {
//      return test;
//  }
//
//  public void setTest(String test) {
//      this.test = test;
//  }
    
    // Openfire
    private @Value("${openfire.customerKey}") String customerKey;
    private @Value("${openfire.customerSecret}") String customerSecret;
    private @Value("${openfire.host}") String host;
    private @Value("${openfire.port}") int port;
    private @Value("${openfire.clientPort}") int clientKey;
    
    
    
    public int getClientKey() {
        return clientKey;
    }
    public void setClientKey(int clientKey) {
        this.clientKey = clientKey;
    }
    public String getCustomerKey() {
        return customerKey;
    }
    public void setCustomerKey(String customerKey) {
        this.customerKey = customerKey;
    }
    public String getCustomerSecret() {
        return customerSecret;
    }
    public void setCustomerSecret(String customerSecret) {
        this.customerSecret = customerSecret;
    }
    public String getHost() {
        return host;
    }
    public void setHost(String host) {
        this.host = host;
    }
    public int getPort() {
        return port;
    }
    public void setPort(int port) {
        this.port = port;
    }
    
    
    
    

}

pom:我已将它包含在 pom 中

<configuration>
        <excludes>
            <exclude>**/*.properties</exclude>
        </excludes>
    </configuration>

我的问题是如何让这个config 文件从项目外的另一个位置读取application.properties。 谢谢

【问题讨论】:

    标签: java spring-boot configuration application.properties


    【解决方案1】:

    可以说,您的应用程序需要外部属性,例如 application.properties 和另一个属性文件 myapp.properties。这两个属性可以在同一个文件夹中,也可以在不同的文件夹中。有 3 种方法。

    命令行参数

    在第一种方法中,您只需将文件夹名称和属性名称作为命令行参数的一部分传递,如下所示:

    java -jar myapp.jar --spring.config.name=application,myapp
    --spring.config.location=classpath:/data/myapp/config,classpath:/data/myapp/external/config
    

    环境变量

    在第二种方法中,您可以将外部化配置详细信息配置到环境变量中,您的 Spring Boot 应用程序将从您的环境中读取它,如下所示:

    set SPRING_CONFIG_NAME=application,myapp
     
    set SPRING_CONFIG_LOCATION=classpath:/data/myapp/config,classpath:/data/myapp/external/config
     
    java -jar myapp.jar
    

    以编程方式加载配置

    package com.java2novice.springboot;
     
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.core.env.ConfigurableEnvironment;
     
    @SpringBootApplication
    public class SpringBootWebApplication {
     
        private static Logger logger = LoggerFactory.getLogger(SpringBootWebApplication.class);
     
        public static void main(String[] args) throws Exception {
     
            ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(SpringBootWebApplication.class)
                    .properties("spring.config.name:application,myapp",
                            "spring.config.location:classpath:/data/myapp/config,classpath:/data/myapp/external/config")
                    .build().run(args);
     
            ConfigurableEnvironment environment = applicationContext.getEnvironment();
     
            logger.info(environment.getProperty("cmdb.resource-url"));
        }
    }
    

    【讨论】:

      【解决方案2】:

      请在使用外部 application.properties 时参考以下链接了解所有可能的选项 -

      https://docs.spring.io/spring-boot/docs/2.5.6/reference/htmlsingle/#features.external-config.files

      【讨论】:

        【解决方案3】:

        通过使用注解PropertySource

        如果文件在类路径中:

         @Configuration
         @PropertySource(value="classpath:org/springframework/context/annotation/p1.properties")
         public class Config {
         }
        

        如果文件是外部的:

        @PropertySource("file:/external/path/to/application.properties")
        public class Config {
        }
        

        【讨论】:

          【解决方案4】:

          根据spring文档:

          具体到你的问题,你可以使用这个解决方案:Application property files

          Spring Boot 使用一个非常特殊的 PropertySource 顺序,旨在允许合理地覆盖值,按以下顺序考虑属性:

          1.命令行参数。

          2.Java系统属性(System.getProperties())。

          3.OS环境变量。

          4.@PropertySource @Configuration 类上的注释。

          5 打包 jar 之外的应用程序属性(application.properties 包括 YAML 和配置文件变体)。

          6.应用程序属性打包在您的 jar 中(application.properties 包括 YAML 和配置文件变体)。

          7.默认属性(使用SpringApplication.setDefaultProperties指定)。

          这里的每个配置都有来自 spring it-self 的详细文档:features-external-config

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-09-12
            • 1970-01-01
            • 2017-06-17
            • 1970-01-01
            • 2018-02-19
            • 1970-01-01
            • 2018-09-15
            • 2014-12-16
            相关资源
            最近更新 更多