How to import a properties file and access a property while using Java configuration to configure Spring.
I want to do all in java. Is there a way to do it?
I tried to use @ImportResource("classpath:config.properties")
but did not work.
Imagine you have an external config file: application-external. yml in the conf/ dir under your home directory, just add it like this: -Dspring. config. location=file:${home}/conf/application-external.
Actually, the most easiest way is to put application. properties and your. jar into the same directory, and just java -jar your. jar will automatically load this external config file.
I've done this on my @Configuration
class using:
@PropertySource(value="classpath:application.properties")
You can get the properties in number a number of ways:
Inject Environment
into configuration beans that need the properties and use environment.getProperty("my.property.value")
, or
Annotate a property with @Value
as outlined here.
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