简体   繁体   中英

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

I have 3 .properties files for one application. 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.

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.

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:

java -DpropertiesType=test ...

The properties overloading and merging is effectively done in Spring framework .

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'. 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. For instance, with Spring and Java Config you would do:

@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
    {    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM