Loading environment specific properties file in spring
Asked Answered
T

2

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.

Tabret answered 21/12, 2015 at 10:58 Comment(2)
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.Welloiled
read this thread here - https://mcmap.net/q/87068/-using-env-variable-in-spring-boot-39-s-application-propertiesDextran
R
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.

Reservoir answered 21/12, 2015 at 12:17 Comment(0)
M
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
Medeah answered 4/2, 2020 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.