Spring 3.1中的默认配置文件

在我的应用程序中,我使用 @Profile("prod")@Profile("demo")注释 bean。 正如您所猜测的:) ,第一个用于连接到产品 DB 的 bean,第二个用于注释使用假 DB (HashMap或其他)的 bean,以加快开发速度。

我想有的是默认配置文件("prod") ,将使用总是,如果它不是由“ 别的东西”覆盖。

最好是在我的 web.xml:

<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</context-param>

然后用 -Dspring.profiles.active="demo"覆盖它,这样我就可以:

mvn jetty:run -Dspring.profiles.active="demo".

但遗憾的是,这并没有奏效。知道我怎么才能做到吗?在所有环境中设置 -Dspring.profiles.active="prod"不是一个选项。

69942 次浏览

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

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

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.

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")

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));
}
}

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.