Config
package com.minte9.collections.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Config {
public static void main(String[] args) throws IOException {
InputStream input = Config.class
.getClassLoader()
.getResourceAsStream("config.properties");
if (input == null) {
throw new RuntimeException("config.properties not found");
}
Properties props = new Properties();
props.load(input);
props.list(System.out);
System.out.println(props.getProperty("db.user"));
System.out.println(props.getProperty("db.port"));
}
}
MultiConfig
package com.minte9.collections.properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class MultiConfig {
public static void main(String[] args) throws IOException {
String env = System.getProperty("env", "dev");
System.out.println("Environment: " + env);
Properties props = new Properties();
loadProps(props, "database-" + env + ".properties");
System.out.println(props.getProperty("db.url"));
System.out.println(props.getProperty("db.user"));
}
private static void loadProps(Properties props, String filename) throws IOException {
InputStream input = MultiConfig.class
.getClassLoader()
.getResourceAsStream(filename);
if (input == null) {
throw new RuntimeException("Propertis file not found: " + filename);
}
props.load(input);
}
}