我有一個 spring boot 應用程式,我想從中排除application.propertiesspring boot 中默認創建的應用程式并從另一個位置讀取它。但是我注意到下面顯示的組態檔將始終嘗試獲取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從專案外的另一個位置讀取。謝謝
uj5u.com熱心網友回復:
可以說,您的應用程式需要外部屬性,如 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"));
}
}
uj5u.com熱心網友回復:
使用外部 application.properties 時,請參閱以下所有可能選項的鏈接 -
https://docs.spring.io/spring-boot/docs/2.5.6/reference/htmlsingle/#features.external-config.files
uj5u.com熱心網友回復:
通過使用注釋 PropertySource
如果類路徑中的檔案:
@Configuration
@PropertySource(value="classpath:org/springframework/context/annotation/p1.properties")
public class Config {
}
如果檔案是外部的:
@PropertySource("file:/external/path/to/application.properties")
public class Config {
}
uj5u.com熱心網友回復:
根據 spring 檔案:
針對您的問題,您可以使用此解決方案:應用程式屬性檔案
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/381712.html
