2012-04-06 249 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配置文件。