spring framework应用程序的多组配置

spring framework应用程序的多组配置,spring,configuration,playframework,configuration-files,configuration-management,Spring,Configuration,Playframework,Configuration Files,Configuration Management,玩!框架提供了一个非常好的特性,使您能够配置不同的配置集,因此我可以 storage=fs storage.fs.home.dir=... storage.fs.home.url=... ... %at.storage=s3 %at.storage.s3.key=... %at.storage.s3.secret=... ... %prod.storage=s3 %prod.storage.s3.key=... %prod.storage.s3.secret=... ... 默认情况下,应

玩!框架提供了一个非常好的特性,使您能够配置不同的配置集,因此我可以

storage=fs
storage.fs.home.dir=...
storage.fs.home.url=...
...

%at.storage=s3
%at.storage.s3.key=...
%at.storage.s3.secret=...
...

%prod.storage=s3
%prod.storage.s3.key=...
%prod.storage.s3.secret=...
...
默认情况下,应用程序是使用fs(文件系统)存储,如果应用程序使用
-%at
(验收测试)模式启动,则使用aws s3作为存储实现,如果应用程序使用
-%prod
生产模式启动,则也使用s3存储,但可以使用不同的s3配置。此功能使应用程序配置文件的版本控制变得非常容易,部署到实时服务器或验收测试服务器时不需要更新配置文件

我很好奇是否有人为基于Spring框架的应用程序实现了某种机制。

看看Spring的


在我的应用程序中,我通常有一个default.properties和类似dev.properties的东西,它覆盖default.properties中的一些值。我使用启用此功能。

像这样尝试,在类路径的根目录中创建不同的目录,包含不同环境的配置。保持相同的文件名:

.
|____pom.xml
|____src
| |____main
| | |____resources
| | | |____dev 
| | | | |____application.properties
| | | |____test 
| | | | |____application.properties
| | | |____prod 
| | | | |____application.properties
然后在应用程序启动时,传入一个环境变量,该变量表示要使用哪个环境。例如:

-Denv=prod
根据环境变量,使用util:properties配置给定属性文件的加载,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd">

    <util:properties id="application" location="classpath:${env}/application.properties"/>

    ... remaining config here

</beans>

... 此处剩余配置

如果我想在不同的上下文中将bean配置为不同的类,该怎么办?e、 我有两套存储配置,一套是在dev上下文中使用的文件系统,另一套是在testing和prod中使用的S3。XML conf是:,但在testing和prod上下文中,它应该是