[英]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.