2

我参考了@Value此处记录的 Spring 注释:@Value和 Spring 配置文件。

我需要能够为给定的属性设置不同的值,例如:

websiteContext=http://localhost:8080/kadjoukor

...根据应用程序是在本地运行还是在云上运行。我不知道如何通过@Value("${websiteContext}")注释来实现这一点......

处理此类问题的最佳做法是什么?

4

1 回答 1

2

如果您使用的是 Spring 3.1 或更高版本,则可以利用 bean 配置文件和 CloudFoundry“云”配置文件根据环境加载不同的属性文件。在 Spring XML 配置文件中可能看起来像这样:

<beans profile="default">
    <context:property-placeholder location="default.properties"/>
</beans>
<beans profile="cloud">
    <context:property-placeholder location="cloud.properties"/>
</beans>

这里有一些很好的博客文章,更详细地解释了它是如何工作的:

于 2012-12-17T15:18:45.820 回答