Confused about java properties file location

前端 未结 4 2117
走了就别回头了
走了就别回头了 2020-12-29 22:14

I have simple java project with structure:

package com.abc: a.java b.java c.properties

I have database configuration parameters conf

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 22:35

    The typical way of handling this is to load the base properties from your embedded file, and allow users of the application to specify an additional file with overrides. Some pseudocode:

    Properties p = new Properties();
    InputStream in = this.getClass().getResourceAsStream("c.properties");
    p.load(in);
    
    String externalFileName = System.getProperty("app.properties");
    InputStream fin = new FileInputStream(new File(externalFileName));
    p.load(fin);
    

    Your program would be invoked similar to this:

    java -jar app.jar -Dapp.properties="/path/to/custom/app.properties"
    

提交回复
热议问题