Spring配置文件可以选择@PropertySources吗? [英] Can @PropertySources be chosen by Spring profile?

查看:404
本文介绍了Spring配置文件可以选择@PropertySources吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring 3.1 @Configuration,它需要一个属性foo来构建一个bean.该属性在defaults.properties中定义,但是如果应用程序具有活动的override Spring概要文件,则该属性可以被overrides.properties中的属性覆盖.

I have a Spring 3.1 @Configuration that needs a property foo to build a bean. The property is defined in defaults.properties but may be overridden by the property in overrides.properties if the application has an active override Spring profile.

在没有覆盖的情况下,代码看起来像这样,并且可以正常工作...

Without the override, the code would look like this, and work...

@Configuration
@PropertySource("classpath:defaults.properties")
public class MyConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public Bean bean() {
        ...
        // this.environment.getRequiredProperty("foo");
        ...
    }
}

我希望@PropertySource用于classpath:overrides.properties,具体取决于@Profile("overrides").是否有人对如何实现这一目标有任何想法?我考虑过的某些选项是重复的@Configuration,但是会违反DRY或对ConfigurableEnvironment的编程操作,但是我不确定environment.getPropertySources.addFirst()调用的去向.

I would like a @PropertySource for classpath:overrides.properties contingent on @Profile("overrides"). Does anyone have any ideas on how this could be achieved? Some options I've considered are a duplicate @Configuration, but that would violate DRY, or programmatic manipulation of the ConfigurableEnvironment, but I'm not sure where the environment.getPropertySources.addFirst() call would go.

如果直接用@Value注入属性,则将以下内容置于XML配置中有效,但是当我使用EnvironmentgetRequiredProperty()方法时则无效.

Placing the following in an XML configuration works if I inject the property directly with @Value, but not when I use Environment and the getRequiredProperty() method.

<context:property-placeholder ignore-unresolvable="true" location="classpath:defaults.properties"/>

<beans profile="overrides">
    <context:property-placeholder ignore-unresolvable="true" order="0"
                                  location="classpath:overrides.properties"/>
</beans>

更新

如果您现在尝试这样做,请查看Spring Boot的@PropertySource支持.

If you're trying to do this now, check out Spring Boot's YAML support, particularly the 'Using YAML instead of Properties' section. The profile support there would make this question moot, but there isn't @PropertySource support yet.

推荐答案

在静态内部类中添加重写的@PropertySource.不幸的是,您必须一起指定所有属性源,这意味着创建一个默认"配置文件来替代替代".

Add the overriding @PropertySource in a static inner class. Unfortunately, you must specify all property sources together which means creating a "default" profile as the alternative to "override".

@Configuration
public class MyConfiguration
{
    @Configuration
    @Profile("default")
    @PropertySource("classpath:defaults.properties")
    static class Defaults
    { }

    @Configuration
    @Profile("override")
    @PropertySource({"classpath:defaults.properties", "classpath:overrides.properties"})
    static class Overrides
    {
        // nothing needed here if you are only overriding property values
    }

    @Autowired
    private Environment environment;

    @Bean
    public Bean bean() {
        ...
        // this.environment.getRequiredProperty("foo");
        ...
    }
}

这篇关于Spring配置文件可以选择@PropertySources吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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