[英]Default profile in Spring 3.1
在我的應用程序中,我使用@Profile("prod")
和@Profile("demo")
注釋了 bean。 第一個,正如您猜到的那樣:),用於連接到生產數據庫的 bean,第二個用於注釋使用一些假數據庫( HashMap
或其他)的 bean - 以加快開發速度。
我想要的是默認配置文件( "prod"
),如果它沒有被“ something-else ”覆蓋,它將始終被使用。
完美的是在我的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"
不是一個選項。
在 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
我的經驗是使用
@Profile("default")
如果沒有識別出其他配置文件,bean 只會被添加到上下文中。 如果您傳入不同的配置文件,例如-Dspring.profiles.active="demo"
,該配置文件將被忽略。
我有同樣的問題,但我使用WebApplicationInitializer以編程方式配置 ServletContext (Servlet 3.0+)。 所以我做了以下事情:
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));
}
}
您也可以考慮刪除 PROD 配置文件,並使用 @Profile("!demo")
關於設置默認生產配置文件已經發布@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>
Spring 在確定哪些配置文件處於活動狀態時提供兩個單獨的屬性:
spring.profiles.active
和
spring.profiles.default
如果設置了spring.profiles.active
,則其值決定了哪些配置文件處於活動狀態。 但是,如果未設置spring.profiles.active
,則 Spring 會查找spring.profiles.default.
如果spring.profiles.active
和spring.profiles.default
都沒有設置,那么就沒有活動的配置文件,只有那些沒有被定義為在配置文件中的 bean 被創建。任何沒有指定配置文件的 bean 都屬於default
配置文件。
您可以將 web.xml 設置為過濾資源,並使用 maven 配置文件設置中的 maven 填充此值 - 這就是我們所做的。
在 pom 中過濾所有資源(如果你沒有 ${} 標記,你可以這樣做)
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
在 web.xml 放
<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
或者簡單地使用任何 maven 命令的-P DEMO
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.