tunetopj
tunetopj

Reputation: 581

Separate properties file for multiple environments

I am trying to have separate property files for prod and dev environment.

I have two property files application-prod.properties, application-dev.properties placed in classpath:/config

I added VM option -Dspring.profiles.active=dev

According to what I understand from the documentation and many other references on the web, on accessing Spring Environment as environment.getProperty("") the property in "application-dev.properties" should be loaded. However, I am getting null and as it seems the property files are not read by Spring.

I also tried defining both the files in @PropertySource. Doing this, the file defined second is picked up and the corresponding property is returned. Spring is not choosing the file based on the active profile.

Am I missing something?

I also came across a issue raised through some SO questions, but I am not sure if it refers to the same problem.

Upvotes: 4

Views: 2238

Answers (1)

luboskrnac
luboskrnac

Reputation: 24561

Right, so documentation you are pointing to is from Spring Boot project. That is not the same as Spring Framework. If you are not using Spring Boot, -Dspring.profiles.active=dev wouldn't work.

You have two options:

  1. Introduce Spring Boot to your project ans turn on auto-configuration (@SpringBootApplication or @EnableAutoConfiguration).
  2. Use plain Spring Framework features like PropertyPlaceholderConfigurer, but it doesn't give you same flexibility as Spring Boot features and you will need to create some boilerplate code to handle various envs.

Upvotes: 3

Related Questions