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