2012-04-06 144 views
91

在我的应用程序中,我有用@Profile("prod")@Profile("demo")注解的beans。 第一个,你可以猜到:)用于连接到生产数据库的bean,第二个注释使用一些假DB(HashMap或其他)的bean - 以加快开发速度。Spring 3.1中的默认配置文件

我想吃点什么是将要始终使用,如果它不被“东西,别的”覆盖默认的配置文件("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"不是一种选择。

回答

57

我的经验是使用

@Profile("default") 

豆,如果没有其他的个人资料被标识将只被添加到上下文。如果您通过其他个人资料,例如-Dspring.profiles.active="demo",此配置文件被忽略。

+4

接受的答案取决于web.xml(并且很好),但是此答案适用于你有没有web.xml,所以对每个人都有更广泛的用处。 – Jay 2015-04-12 16:10:58

+1

这个解决方案更清洁 – cahen 2015-04-28 14:07:01

+0

这是官方功能还是一些副作用?你想链接到描述这个特性的Spring文档吗? – rustyx 2015-09-17 11:50:07

108

定义您的生产环境为默认的配置文件在web.xml

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

,如果你想使用一个不同的配置文件把它作为系统属性

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

没有他试图定义** **活跃轮廓在web.xml和系统属性。在我的解决方案中,我在web.xml中配置一个**默认**配置文件,并通过系统属性覆盖/定义* active *配置文件。如果没有明确的*激活*配置文件,将使用默认值。 – andih 2012-04-06 10:01:43

+0

谢谢!这正是我想要的!不能在任何地方找到它:/ – 2012-04-06 10:04:19

+0

的一个问题这种方法:如果你在'application.properties'设置'spring.profiles.default = prod',然后'应用prod.properties'不会被加载。这是违反直觉的。 – gamliela 2016-07-31 08:55:00

-1

您可以设置您的网页。 xml作为已过滤的资源,并通过maven配置文件设置的maven填充此值 - 即我们所做的。

在POM过滤

在web.xml中的所有资源(你可以做taht如果你没有$ {}在它们标记)

<webResources> 
    <resource> 
     <directory>src/main/webapp</directory> 
     <filtering>true</filtering> 
    </resource> 
</webResources> 

<context-param> 
    <param-name>spring.profiles.active</param-name> 
    <param-value>${spring.prfile}</param-value> 
</context-param> 

在POM创建Maven的配置文件

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

现在你可以使用

mvn jetty:run -P DEMO 

或者干脆-P DEMO任何Maven的命令

+1

我不知道,但我认为这将无法正常工作。恕我直言码头:运行不会运行资源被过滤的阶段。 caurse的 – 2012-04-06 11:14:22

+0

你需要运行mvn清洁编译码头:运行-P DEMO,但是未编译的代码运行时,它autimaticly – Hurda 2012-04-06 19:45:36

+10

据我了解,春季3.1型材的主要目标之一是产生一个WAR文件准备好进行部署所有环境。使用Maven配置文件是退回到以前的状态:每个环境需要打包WAR文件... – edrabc 2012-09-03 22:54:21

3

关于设置已经发布@andih

设置对Maven插件码头的默认配置文件最简单的方式默认的生产概况,是包含以下元素在你的插件配置:

<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> 
5

您也可以考虑删除PROD配置文件,并使用@Profile(“!演示“)

+2

我想这不会工作,如果你有两个以上的配置文件,对不对? – Chop 2015-09-07 11:58:27

5

我有同样的问题,但我为了配置编程ServletContext中(Servlet的3.0+)使用WebApplicationInitializer所以我做到以下几点:。

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

春天提供两个单独的属性决定何时哪些profile活性:

  • spring.profiles.active

  • spring.profiles.default

如果spring.profiles.active被设置,那么它的值确定了轮廓是活动的。但是,如果spring.profiles.active没有设置,那么Spring看起来spring.profiles.default.

如果没有spring.profiles.active也不spring.profiles.default被设置,那么有没有被定义为一个配置文件是没有作用轮廓,只有那些豆created.Any未指定配置文件的Bean属于default配置文件。