I have a Spring project which uses another project. Each project has its own spring profile initialize from java code using applicationContext.xml
and *.p
SPRING_PROFILES_ACTIVE is the environment variable to override/pick Spring profile
My solution is to set the environment variable as spring.profiles.active=development
. So that all applications running in that machine will refer the variable and start the application. The order in which spring loads a properties as follows
application.properties
system properties
environment variable
I normally configure the applicationContext using Annotation based configuration rather than XML based configuration. Anyway, I believe both of them have the same priority.
*Answering your question, system variable has higher priority *
@Component
@Profile("dev")
public class DatasourceConfigForDev
Now, the profile is dev
Note : if the Profile is given as
@Profile("!dev")
then the profile will exclude dev and be for all others.
<beans profile="dev">
<bean id="DatasourceConfigForDev" class="org.skoolguy.profiles.DatasourceConfigForDev"/>
</beans>
@Configuration
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("spring.profiles.active", "dev");
}
}
@Autowired
private ConfigurableEnvironment env;
// ...
env.setActiveProfiles("dev");
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-config.xml</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
The profile names passed as the parameter will be activated during application start-up:
-Dspring.profiles.active=dev
In IDEs, you can set the environment variables and values to use when an application runs. The following is the Run Configuration in Eclipse:
to set via command line : export spring_profiles_active=dev
Any bean that does not specify a profile belongs to “default” profile.
If you provide your JVM the Spring profile there should be no problems:
java -Dspring.profiles.active=development -jar yourApplication.jar
Also see Spring-Documentation:
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
69.5 Set the active Spring profiles
The Spring Environment has an API for this, but normally you would set a System property (spring.profiles.active) or an OS environment variable (SPRING_PROFILES_ACTIVE). E.g. launch your application with a -D argument (remember to put it before the main class or jar archive):
$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar
In Spring Boot you can also set the active profile in application.properties, e.g.
spring.profiles.active=production
A value set this way is replaced by the System property or environment variable setting, but not by the SpringApplicationBuilder.profiles() method. Thus the latter Java API can be used to augment the profiles without changing the defaults.
See Chapter 25, Profiles in the ‘Spring Boot features’ section for more information.
If i run the command line : java -Dspring.profiles.active=development -jar yourApplication.jar
from my webapplication directory it states that the path is incorrect. So i just defined the profile in manualy in the application.properties file like this :
spring.profiles.active=mysql
or
spring.profiles.active=postgres
or
spring.profiles.active=mongodb
If you are using docker to deploy the spring boot app, you can set the profile using the flag e:
docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8080:8080 -t r.test.co/myapp:latest