[英]Is it possible to combine 3 .properties files into one .properties file?
我有一個應用程序3個.properties文件。 每個文件都用於開發,測試和生產環境。 是否可以(實用)將這三個文件合並為一個文件? 如何做呢? 還是最好將每個文件保存在自己的環境中? 這是代碼。
enter code here
lock-timeout=7200000
usesLogin=true
uses-hardlocks=false
use-nested-roles=1
# Password Change URL for VSRD, VSRT and VSRP (in that order)
#pwchange-url=https://iteodova-md.dev.fema.net/va-npsc/pwchange/default.asp
#pwchange-url=https://iteodova-md.dev.fema.net/va-npsc/pwchange_tdl/default.asp
pwchange-url=https://iteodova-md.fema.net/va-npsc/pwchange/default.asp
# Database Connectivity and User Session Management
jdbc-driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc-url=jdbc:oracle:thin:@wnli3d3.fema.net:1521:vsrd
#jdbc-url=jdbc:oracle:thin:@wnli3d2.fema.net:1521:vsrt
#jdbc-url=jdbc:oracle:thin:@mwli3d1.fema.net:1521:vsrp
jdbc-url=jdbc:oracle:thin:@wnli3d4.fema.net:1521:vsrp
您可以使用Apache的Commons Configuration 。
例如:
CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(new PropertiesConfiguration("color.properties");
config.addConfiguration(new PropertiesConfiguration("application.properties"));
或如本例所示:
# usergui.properties
include = colors.properties
include = sizes.properties
您可以將這種屬性與資格一起使用。
在以下代碼中,您可以擁有
key=value
作為默認值,就像現在一樣
key.ENVIRON=value
如果該環境需要與眾不同。
如您所見,該代碼非常短,並且由於它們位於一個位置,因此可以輕松比較不同的配置。
如果您有復雜的系統,則可以使用ENVIRON.TYPE.INSTANCE擴展此方法。
public class EnvironProperties extends Properties {
private final String environ;
public EnvironProperties(String environ) {
this.environ = environ;
}
@Override
public String getProperty(String key) {
String property = super.getProperty(key + "." + environ);
return property == null ? super.getProperty(key) : property;
}
public String getEnviron() {
return environ;
}
public static void main(String... args) throws IOException {
String properties = "lock-timeout=7200000\n" +
"usesLogin=true\n" +
"uses-hardlocks=false\n" +
"use-nested-roles=1\n" +
"\n" +
"# Password Change URL for VSRD, VSRT and VSRP (in that order)\n" +
"pwchange-url=https://iteodova-md.dev.fema.net/va-npsc/pwchange/default.asp\n" +
"pwchange-url.PROD=https://iteodova-md.fema.net/va-npsc/pwchange/default.asp\n" +
"\n" +
"# Database Connectivity and User Session Management\n" +
"\n" +
"jdbc-driverClassName=oracle.jdbc.driver.OracleDriver\n" +
"jdbc-url.TEST=jdbc:oracle:thin:@wnli3d3.fema.net:1521:vsrd\n" +
"jdbc-url.DEV=jdbc:oracle:thin:@wnli3d2.fema.net:1521:vsrt\n" +
"jdbc-url.PROD=jdbc:oracle:thin:@mwli3d1.fema.net:1521:vsrp\n" +
"jdbc-url=jdbc:oracle:thin:@wnli3d4.fema.net:1521:vsrp";
EnvironProperties dev = new EnvironProperties("DEV");
EnvironProperties test = new EnvironProperties("TEST");
EnvironProperties prod = new EnvironProperties("PROD");
for (EnvironProperties ep : new EnvironProperties[]{dev, test, prod}) {
System.out.println("[" + ep.getEnviron() + "]");
ep.load(new StringReader(properties));
for (String prop : "uses-hardlocks,pwchange-url,jdbc-url".split(","))
System.out.println(prop + "= " + ep.getProperty(prop));
}
}
}
我同意最好將屬性保留在單獨的文件中的想法。 如果您使用環境變量或配置文件(如果您使用的是Spring),則可以將文件物理上分成單獨的目錄,例如“ src / main / resources”。 然后,您只需要根據環境變量或配置文件設置閱讀正確的內容即可。 如何執行取決於Java堆棧的具體情況。 例如,使用Spring和Java Config,您可以執行以下操作:
@Configuration
public class MyMainConfig {
public MyMainConfig() {
super();
}
@Configuration
@Profile("dev")
@PropertySource({ "classpath:/dev/myPropertyFileName.properties" })
static class Dev
{ }
@Configuration
@Profile("test")
@PropertySource({ "classpath:/test/myPropertyFileName.properties" })
static class Test
{ }
@Configuration
@Profile("prod")
@PropertySource({ "classpath:/prod/myPropertyFileName.properties" })
static class Prod
{ }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.