如何在Spring Bean文件中使用Maven配置文件ID值? [英] How can I use maven profile Id value in spring bean files?
问题描述
mvn -P dev
如果我使用概要文件dev来构建项目,那么我想在我的spring bean中使用dev.properties,如下所示.是否有可能 ?如果是这样,我如何获得个人资料名称?
If I build my project using profile dev, then I want to use dev.properties in my spring bean like below. Is it possible ? If so , how could I get profile name ?
<bean id="xyz" class="abc.xyz">
<property name="propertyFile" value="${maven_profile_id}.properties" />
</bean>
先谢谢了.
推荐答案
您可以使用Maven配置文件向构建中添加配置文件"属性:
You can use Maven profiles to add a 'profile' property to the build:
<profiles>
<profile>
<id>dev</id>
<properties>
<profile>dev</profile>
</properties>
</profile>
</profiles>
然后使用系统属性将该值传递到您的应用程序中,这是带有surefire的示例:
Then pass the value into your application using a system property, here's an example with surefire:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<profile>${profile}</profile>
</systemPropertyVariables>
</configuration>
</plugin>
最后,可以在您的应用程序中引用它:
Finally this can be referenced in you application:
<bean id="xyz" class="abc.xyz">
<property name="propertyFile" value="${profile}.properties" />
</bean>
或者,如果您使用的是Spring 3.1或更高版本,则可能会找到
Alternatively, if you are using Spring 3.1 or later you might find the XML profile feature meets your needs (although it may be overkill).
这篇关于如何在Spring Bean文件中使用Maven配置文件ID值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!