根据配置文件在Spring中加载属性文件 [英] Load properties file in Spring depending on profile

查看:148
本文介绍了根据配置文件在Spring中加载属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring 3.1应用程序。假设它有一个包含以下内容的XML:

I have a Spring 3.1 application. Let's say it has an XML with the following content:

<context:property-placeholder location="classpath:somename.properties" />

<context:property-placeholder location="classpath:xxx.properties" />

我希望some.properties总是被加载(让我们假设它存在),但是xxx部分根据活动配置文件,第二个占位符被某个名称替换。我试过这个:

I would like some.properties to be always loaded (let's assume it exists), but the xxx part of the second place holder to be replaced by some name depending on the active profile. I've tried with this:

<beans profile="xx1">
    <context:property-placeholder location="classpath:xx1.properties" />
</beans>

<beans profile="xx2">
    <context:property-placeholder location="classpath:xx2.properties" />
</beans>

此外,两个文件都具有相同密钥但价值不同的属性。

Also, both files have properties with the same key but different value.

但是它不起作用,因为一些后来的bean有一个占位符用于一个属性,其关键字在xx1.properties(和xx2.properties)中定义,使得Spring抱怨该密钥在应用程序上下文。

But it didn't work as some later bean that has a placeholder for one property whose key is defined in xx1.properties (and xx2.properties) makes Spring complain that the key is not found in the application context.

推荐答案

我已决定提交并回答此问题,因为它尚未被接受。它可能不是你正在寻找的具体但它适用于我。另请注意,我正在使用新的注释驱动配置,但它可以移植到xml配置。

I have decided to submit and answer to this as it has not yet been accepted. It may not be what you are looking for specifically but it works for me. Also note that i am using the new annotation driven configuration however it can be ported to the xml config.

我有一个每个环境的属性文件(dev.properties,test .properties等)

I have a properties file for each environment(dev.properties, test.properties etc)

然后我有一个RootConfig类,它是用于所有配置的类。这个类中包含的所有内容都是两个注释:@Configuration和@ComponentScan(basePackageClasses = RootConfig.class)。
这告诉它扫描与它相同的包中的任何内容。

I then have a RootConfig class that is the class that is used for all the configuration. All that this class has in it is two annotations: @Configuration and @ComponentScan(basePackageClasses=RootConfig.class). This tells it to scan for anything in the same package as it.

然后有一个配置包含我所有正常配置。对于与上面的根配置类相同的包中的每个环境,还有一个配置。

There is then a Configuration Containing all my normal configuration sitting wherever. There is also a configuration for each environment in the same package as the root configuration class above.

特定于环境的配置只是具有以下注释的标记类以指向它到特定于环境的属性文件:

The environment specific configurations are simply marker classes that have the following annotations to point it to the environment specific properties files:

@Configuration
@PropertySource("classpath:dev.properties")
@Import(NormalConfig.class)
@Profile("dev")

import告诉它引入正常的配置类。但当它进入那里时,它将设置特定于环境的属性。

The import tells it to bring in the normal config class. But when it gets in there it will have the environment specific properties set.

这篇关于根据配置文件在Spring中加载属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆