Reputation: 678
I'd like to set active profile for Spring based on environment variable. To do so, I've written the following code:
@Configuration
public class SpringBootApplicationInitializer
implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) {
String platform = System.getProperty("platform");
if ("prod".equals(platform)) {
servletContext.setInitParameter("spring.profiles.active",
"prod");
} else if ("stag".equals(platform)) {
servletContext.setInitParameter("spring.profiles.active",
"stag");
} else {
servletContext.setInitParameter("spring.profiles.active",
"dev");
}
}
}
This code gets executed, however, no profile is set as Spring writes: INFO - No active profile set, falling back to default profiles: default
How do I achieve what I want?
Upvotes: 0
Views: 1083
Reputation: 130837
How do I achieve what I want?
You could simply set the desired profile in the SPRING_PROFILES_ACTIVE
environment variable.
What if I don't want to set this variable for
dev
environment? Spring will not pick up active profile then.
To address your comment, there are a number of ways to configure Spring Boot applications. If your concern is not using this environment variable for development, you'll be please to know that you can still use a properties or YAML file for development.
Just bear in mind that OS environment variables take precedence over application properties (application.properties
and YAML variants). For details all configuration property sources, refer to the documentation.
Upvotes: 2
Reputation: 9437
Try like like this in application.yml
spring:
profiles:
active: ${platform}
Upvotes: 0