简体   繁体   English

如何从 spring 引导应用程序之外的位置读取 application.properties 文件

[英]How to read the application.properties file from a location outside the spring boot application

I have a spring boot application from which I want to exclude the application.properties created by default in spring boot and read it from another location.我有一个 spring 引导应用程序,我想从中排除在 spring 引导中默认创建的application.properties并从另一个位置读取它。 However I noticed that the configuration file shown below, will always try to fetch for the application.properties file inside the project.但是我注意到下面显示的配置文件将始终尝试获取项目中的application.properties文件。

Config.java file: 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 : i have included this in pom pom :我已经在 pom 中包含了这个

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

my question is how to make this config file read the application.properties from another location outside the project.我的问题是如何让这个config文件从项目外的另一个位置读取application.properties Thank you谢谢

By using the annotation PropertySource通过使用注解PropertySource

If file within classpath:如果类路径中的文件:

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

If file is external:如果文件是外部的:

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

Please refer to below link on all possible options while using external application.properties -使用外部 application.properties 时,请参阅以下所有可能选项的链接 -

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

According to spring documentation:根据 spring 文档:

Specific to you question you can use this solution: Application property files具体到您的问题,您可以使用此解决方案: 应用程序属性文件

Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values, properties are considered in the the following order: Spring 引导使用非常特殊的PropertySource顺序,旨在允许合理覆盖值,按以下顺序考虑属性:

1.Command line arguments. 1.命令行arguments。

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

3.OS environment variables. 3.OS环境变量。

4. @PropertySource annotations on your @Configuration classes. 4. @Configuration 类上的@PropertySource注释。

5 Application properties outside of your packaged jar ( application.properties including YAML and profile variants). 5 打包后的 jar 之外的应用程序属性( application.properties包括 YAML 和配置文件变体)。

6.Application properties packaged inside your jar ( application.properties including YAML and profile variants). 6. 打包在 jar 中的应用程序属性( application.properties包括 YAML 和配置文件变体)。

7.Default properties (specified using SpringApplication.setDefaultProperties ). 7.默认属性(使用SpringApplication.setDefaultProperties指定)。

For each configuration here is a detailed documentations from spring it-self: features-external-config对于每个配置,这里有来自 spring 的详细文档: features-external-config

lets say, your application requires externalized properties like application.properties and another property file myapp.properties.可以说,您的应用程序需要诸如 application.properties 和另一个属性文件 myapp.properties 之类的外部属性。 The both properties can be in the same folder or they can be in different folder.这两个属性可以在同一个文件夹中,也可以在不同的文件夹中。 There are 3 ways of doing it.有 3 种方法可以做到这一点。

Command line arguments命令行 arguments

In the first approach, all you need to do is pass folder names and property names as part of command line arguments as shown below:在第一种方法中,您只需将文件夹名称和属性名称作为命令行 arguments 的一部分传递,如下所示:

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

Environment variables环境变量

In the second approach, you can configure your externalized configuration details into environment variables and your Spring Boot application will read it from your environment as shown below:在第二种方法中,您可以将外部化配置详细信息配置到环境变量中,您的 Spring 引导应用程序将从您的环境中读取它,如下所示:

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

Programatically loding configurations以编程方式加载配置

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"));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 春季启动:从“ application.properties”读取文件路径 - Spring boot: Read a file PATH from “application.properties” 如何阅读Spring Boot application.properties? - How to read Spring Boot application.properties? 如何在 spring 引导中读取构造函数内的 application.properties 值? - How to read application.properties value inside the constructor in spring boot? Spring 启动应用程序.properties 如何读取日文或片假名字符 - Spring boot application.properties how to read Japanese or Katakana character Spring boot application.properties 文件读取 - Spring boot application.properties file reading Spring 引导无法识别 application.properties 文件 - Spring Boot not recognizing application.properties file 为什么 Spring 引导不从默认的 application.properties 文件中读取 - Why is Spring Boot not reading from default application.properties file 来自application.properties的Spring Boot配置 - Spring Boot configuration from application.properties Spring boot fat jar 无法从 application.properties 中定义的绝对路径读取文件 - Spring boot fat jar cannot read file from absolute path defined in application.properties Spring Boot不会从application.properties文件中读取默认值 - Spring Boot doesn't read the default value from the application.properties file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM