user373201
user373201

Reputation: 11455

Spring Java config: import properties file

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.

Upvotes: 9

Views: 14875

Answers (1)

Alex Barnes
Alex Barnes

Reputation: 7218

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.

Upvotes: 21

Related Questions