Spring项目中的Netflix Archaius属性

Archaius基础

Netflix Archaius是用于管理应用程序配置的库。 考虑一个属性文件“ sample.properties”,其中包含一个名为“ myprop”的属性:

myprop=myprop_value_default

这是使用Archaius加载文件的方式:

ConfigurationManager
                .loadCascadedPropertiesFromResources("sample");

String myProp = DynamicPropertyFactory.getInstance().getStringProperty("myprop", "NOT FOUND").get();

assertThat(myProp, equalTo("myprop_value_default"));

Archaius可以加载适合于环境的属性,请考虑存在一个“ sample-perf.properties”,其具有针对perf环境覆盖的相同配置:

myprop=myprop_value_perf

现在,可以通过在sample.properties文件中添加以下内容来指示Archaius以级联方式加载配置:

myprop=myprop_value_default

@next=sample-${@environment}.properties

测试看起来像这样:

ConfigurationManager.getDeploymentContext().setDeploymentEnvironment("perf");
ConfigurationManager
        .loadCascadedPropertiesFromResources("sample");

String myProp = DynamicPropertyFactory.getInstance().getStringProperty("myprop", "NOT FOUND").get();

assertThat(myProp, equalTo("myprop_value_perf"));

Spring物业基础

Spring属性基础在此处的Spring Framework参考站点中有很好的解释。 简而言之,如果有一个属性文件“ sample.properties”,则可以通过以下方式加载和引用该文件:

@Configuration
@PropertySource("classpath:/sample.properties")
public class AppConfig {
    @Autowired
    Environment env;

    @Bean
    public TestBean testBean() {
        TestBean testBean = new TestBean();
        testBean.setName(env.getProperty("myprop"));
        return testBean;
    }


}

甚至更简单,可以通过以下方式用占位符取消引用它们:

@Configuration
@PropertySource("classpath:/sample.properties")
public class AppConfig {
    @Value("${myprop}")
    private String myProp;

    @Bean
    public TestBean testBean() {
        TestBean testBean = new TestBean();
        testBean.setName(myProp));
        return testBean;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

使Archaius属性对Spring可见

因此,现在的问题是如何在Spring中显示Archaius属性,我所采用的方法是一种快速而肮脏的方法,但是可以根据需要进行清理。 我的方法是定义一个Spring PropertySource ,在内部将其委托给Archaius:

import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicPropertyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.PropertySource;

import java.io.IOException;

public class SpringArchaiusPropertySource extends PropertySource<Void> {


    private static final Logger LOGGER = LoggerFactory.getLogger(SpringArchaiusPropertySource.class);


    public SpringArchaiusPropertySource(String name) {
        super(name);
        try {
            ConfigurationManager
                    .loadCascadedPropertiesFromResources(name);
        } catch (IOException e) {
            LOGGER.warn(
                    "Cannot find the properties specified : {}", name);
        }

    }

    @Override
    public Object getProperty(String name) {
         return DynamicPropertyFactory.getInstance().getStringProperty(name, null).get();
    }
}

棘手的部分是使用Spring注册此新的PropertySource,这可以使用ApplicationContextInitializer来完成,该方法在初始化应用程序上下文之前被触发:

import com.netflix.config.ConfigurationBasedDeploymentContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;

public class SpringProfileSettingApplicationContextInitializer
        implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext ctx) {
        ctx.getEnvironment()
                .getPropertySources()
                .addFirst(new SpringArchaiusPropertySource("samples"));
    }
}

最后在这里描述了如何使用Spring注册这个新的ApplicationContextInitializer。 本质上就是这样,现在Netflix Archaius属性应该可以在Spring应用程序中工作。

翻译自: https://www.javacodegeeks.com/2015/03/netflix-archaius-properties-in-a-spring-project.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值