Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Java config: import properties file

Tags:

spring

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.

like image 393
user373201 Avatar asked Dec 27 '11 18:12

user373201


People also ask

How do I import external properties into Spring boot?

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.

How do you pass the properties file path in Spring boot?

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.


1 Answers

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:

  1. Inject Environment into configuration beans that need the properties and use environment.getProperty("my.property.value"), or

  2. Annotate a property with @Value as outlined here.

like image 135
Alex Barnes Avatar answered Sep 30 '22 15:09

Alex Barnes