Please can you help me to read the properties from application.properties file in Spring Boot, without autowiring the Environment
and without using the Environment
?
No need to use ${propname}
either. I can create properties object but have to pass my properties file path. I want to get my prop file from another location.
One of the easiest ways to read a property from the application. properties file is by autowiring an Environment object. All you need to do is to use the @Autowired annotation. It is called dependency injection.
YAML is a convenient format for specifying hierarchical configuration data. This can be more readable than its property file alternative since it does not contain repeated prefixes.
This is a core Java feature. You don't have to use any Spring or Spring Boot features if you don't want to.
Properties properties = new Properties();
try (InputStream is = getClass().getResourceAsStream("application.properties")) {
properties.load(is);
}
JavaDoc: http://docs.oracle.com/javase/8/docs/api/java/util/Properties.html
OrangeDog solution didn't work for me. It generated NullPointerException.
I've found another solution:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Properties properties = new Properties();
try (InputStream resourceStream = loader.getResourceAsStream("application.properties")) {
properties.load(resourceStream);
} catch (IOException e) {
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With