I read through the Spring Boot documentation for externalized configuration and I see that it automatically loads the src/main/resources/application.properties file which can be then wired to the bean properties using annotation.
However I want to have a generic PropertyHelper
class which can be used to build the java.util.Properties
with the properties in application.properties. Can this be done?
We are currently achieving this manually as below:
public class PropertyHelper {
private static Properties loadProperties() {
try {
String propsName = "application.properties";
InputStream propsStream = PropertyHelper.class
.getClassLoader().getResourceAsStream(propsName);
if (propsStream == null) {
throw new IOException("Could not read config properties");
}
Properties props = new Properties();
props.load(propsStream);
application.properties
– JensEnvironment
you can get the properties, but it doesn't have a list of all properties. you only can useenv.getProperty("propertyName")
to get the property – Arsen DavtyanEnvironment
is very likely aConfigurableEnvironment
, which allows you to iterate the property sources, and you can iterate the properties of anyPropertySource
that is anEnumerablePropertySource
. --- The advantage of usingEnvironment
is that you gain support for features like Profiles and YAML. But the question is: Why do you need to iterate them? Don't you know the names of the properties that are of interest to you? – Andreas