问题
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 connect to production DB and second one annotates beans that use some fake DB (HashMap
or whatever)- to make development faster.
What I would like to have is default profile (\"prod\"
) that will be used always if it is not overridden by \"something-else\".
Perfect would be to have in my web.xml
:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</context-param>
and then override this with -Dspring.profiles.active=\"demo\"
so that I could do:
mvn jetty:run -Dspring.profiles.active=\"demo\".
But sadly this is not working. Any idea how could I achive that? Setting -Dspring.profiles.active=\"prod\"
on all my environments is not an option.
回答1:
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.
回答2:
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
回答3:
You may also consider removing the PROD profile, and use @Profile("!demo")
回答4:
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));
}
}
回答5:
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>
回答6:
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.
回答7:
You can setup your web.xml as filtered resource and have this value filled by maven from maven profile settings - that what we do.
in pom filter all resources (you can do taht if you have no ${} marking in them)
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
in web.xml put
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${spring.prfile}</param-value>
</context-param>
in pom create maven profiles
<profiles>
<profile>
<id>DEFAULT</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profile>prod</spring.profile>
</properties>
<profile>
<profile>
<id>DEMO</id>
<properties>
<spring.profile>demo</spring.profile>
</properties>
<profile>
</profiles>
Now you can use
mvn jetty:run -P DEMO
or simply -P DEMO
with any maven command
来源:https://stackoverflow.com/questions/10041410/default-profile-in-spring-3-1