In my application I have beans annotated with @Profile(\"prod\")
and @Profile(\"demo\")
.
The first one, as you can guess :), is used on beans that
About setting default production profile already posted @andih
The easiest way to set default profile for maven jetty plugin, is to include below element in your plugin configuration:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<systemProperties>
<systemProperty>
<name>spring.profiles.active</name>
<value>demo</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
You may also consider removing the PROD profile, and use @Profile("!demo")
Define your production environment as default profile in your web.xml
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>prod</param-value>
</context-param>
and if you want to use a different profile pass it as system property
mvn -Dspring.profiles.active="demo" jetty:run
Spring provide two separate properties when determining which profiles are active:
spring.profiles.active
and
spring.profiles.default
If spring.profiles.active
is set, then its value determines which profiles are active. But if spring.profiles.active
isn't set, then Spring looks to spring.profiles.default.
If neither spring.profiles.active
nor spring.profiles.default
is set, then there are no active profiles, and only those beans that aren't defined as being in a profile are created.Any bean that does not specify a profile belongs to default
profile.
My experience is that using
@Profile("default")
the bean will only be added to the context if no other profile is identified. If you pass in a different profile, e.g. -Dspring.profiles.active="demo"
, this profile is ignored.
I have the same issue, but I use WebApplicationInitializer in order to configure the ServletContext programmatically (Servlet 3.0+). So I do the following:
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext sc) throws ServletException {
// Create the 'root' Spring application context
final AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
// Default active profiles can be overridden by the environment variable 'SPRING_PROFILES_ACTIVE'
rootContext.getEnvironment().setDefaultProfiles("prod");
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
sc.addListener(new ContextLoaderListener(rootContext));
}
}