2

I am working on a spring-boot application, I need your assistance on below scenario.

I have properties files for each environment something like application-dev.properties, application-prod.properties etc. Is there way that my application can load environment specific properties file by using spring @Profile annotation.

Please help.

Naveen
  • 907
  • 2
  • 15
  • 25
  • Spring boot already does that by default, specify the active profiles at startup and spring boot will load `application.properties` and an `application-{profile}.properties` for you. – M. Deinum Dec 23 '15 at 06:25
  • read this thread here - https://stackoverflow.com/a/45512798/2685581 – Ajay Kumar Aug 05 '17 at 12:35

2 Answers2

0

You don't need to use @Profiles annotation at all. Just use

@ConfigurationProperties(locations = "classpath:myapp-${environment.type}.properties")

and define environment type via system property. E.g. via command line -Denvironment.type=dev.

luboskrnac
  • 23,973
  • 10
  • 81
  • 92
0

@Profile is not for loading environment specific properties file. It is for specifying profile of a bean. For example,

@Profile("dev")
@Component
class Foo {

}

It means the bean of Foo is only available when the active profiles include dev. Or the opposite @Profile("!dev"), which means the bean is available when dev is not an active profile.

So for loading environment specific properties file, since it is spring-boot, you can just specify the active profiles. There are several ways to specify the active profiles.

  • Environment variable: SPRING_PROFILES_ACTIVE=dev,prod
  • command line argument: java -jar app.jar --spring.profiles.active=dev,prod
  • Programmatically : SpringApplicationBuilder(...).properties("spring.profiles.active=dev,prod).run(...)
  • Default application.properties or yaml: spring.profiles.active:dev, prod
geliba187
  • 357
  • 2
  • 11