简体   繁体   English

是否可以将3个.properties文件合并为一个.properties文件?

[英]Is it possible to combine 3 .properties files into one .properties file?

I have 3 .properties files for one application. 我有一个应用程序3个.properties文件。 Each file is for the Development, Test, and Production environment. 每个文件都用于开发,测试和生产环境。 Is it possible (and practical) to combine these three files into one file? 是否可以(实用)将这三个文件合并为一个文件? How can it be done? 如何做呢? Or is it best to keep each file in its own environment? 还是最好将每个文件保存在自己的环境中? Here is the code. 这是代码。

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

You could by using Commons Configuration from Apache. 您可以使用Apache的Commons Configuration

For example: 例如:

CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(new PropertiesConfiguration("color.properties");
config.addConfiguration(new PropertiesConfiguration("application.properties"));

Or include as in this example: 或如本例所示:

# usergui.properties

include = colors.properties
include = sizes.properties

You can use Properties this way with qualifications. 您可以将这种属性与资格一起使用。

In the following code you can have 在以下代码中,您可以拥有

key=value

as the default value, just like you do now and 作为默认值,就像现在一样

key.ENVIRON=value

if it needs to be different for that environment. 如果该环境需要与众不同。

As you can see, the code is very short and it allows to compare different configurations easily because they are all in one place. 如您所见,该代码非常短,并且由于它们位于一个位置,因此可以轻松比较不同的配置。

You can extend this approach with ENVIRON.TYPE.INSTANCE if you have a complex system. 如果您有复杂的系统,则可以使用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));
        }
    }
}

It is good idea to keep the properties in separate files (ie for testing, staging, production etc.) . 最好将属性保存在单独的文件中(即用于测试,登台,生产等)。 The switching among them can be done by running java with -D option: 它们之间的切换可以通过运行带有-D选项的java来完成:

java -DpropertiesType=test ...

The properties overloading and merging is effectively done in Spring framework . Spring框架中有效地完成了属性重载和合并。

You can take a look on this example. 您可以看一下这个例子。

Good luck! 祝好运!

I agree with the idea that it is best to keep the properties maintained in separate files. 我同意最好将属性保留在单独的文件中的想法。 If you use an environment variable or Profile (if you are using Spring), you can separate the files physically into their own directories within say 'src/main/resources'. 如果您使用环境变量或配置文件(如果您使用的是Spring),则可以将文件物理上分成单独的目录,例如“ src / main / resources”。 Then you just need to read in the right one depending upon your environment variable or profile setting. 然后,您只需要根据环境变量或配置文件设置阅读正确的内容即可。 How you do this is dependent on the specifics of your Java stack. 如何执行取决于Java堆栈的具体情况。 For instance, with Spring and Java Config you would do: 例如,使用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.

相关问题 在测试时组合Application.properties文件 - combine Application.properties files when testing java属性文件中是否可以将一个变量指向另一个变量? - Is it possible to one variable points to other in java properties file? 是否可以在.properties文件中划分2个变量 - Is it possible to divide 2 variables in .properties file 可以在文件中写入命令属性 - Possible to write command properties in a file 属性文件中的属性列表 - List of properties in a properties files 可以在.properties文件中更改“绝对”位置吗? - Possible to change the an 'absolute' location in a .properties files? 是否可以使用ant覆盖xml文件中的属性? - Is it possible to override properties in xml-files with ant? Spring-是否可以在属性文件中递归解析属性(例如环境变量)? - Spring - Is it possible to resolve properties (such as environment variables) recursively in properties file? 是否可以将环境变量作为 Kafka kafkaProducer.properties 和 KafkaConsumer.properties 文件中的属性值引用 - is it possible to refer environmental variable as values for properties in the kafkaProducer.properties and KafkaConsumer.properties files of Kafka Java:将多个属性文件作为一个加载 - Java: Load multiple properties files as one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM