I want to load Properties Files in Java code. But I use profile to config -Dspring.profiles.active=local or dev... How to load properties files by profile Something like this:
classpath:${spring.profiles.active}/test.properties
How to do that in Java code ? I did as below, but get null.
Properties prop = new Properties();
InputStream iStream = Helper.class.getClassLoader().getResourceAsStream("test.properties");
try {
prop.load(iStream);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
try {
iStream.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
This is some working code for us:
String activeProfile = System.getProperty("spring.profiles.active");
InputStream workSpacesFIS = this.getClass().getClassLoader()
.getResourceAsStream(activeProfile + "/customers.txt");
if (workSpacesFIS != null) { ...
Loading Java Properties Files by Profile
public Properties getProp() throws IOException {
final Properties prop = new Properties();
prop.load(TestService.class.getResourceAsStream("/application.properties"));
String activeProfile = prop.getProperty("spring.profiles.active");
prop.load(TestService.class.getResourceAsStream("/application-"+activeProfile+".properties"));
return prop;
}