javascript
Spring项目中的Netflix Archaius属性
Archaius基礎(chǔ)
Netflix Archaius是用于管理應(yīng)用程序配置的庫。 考慮一個(gè)屬性文件“ sample.properties”,其中包含一個(gè)名為“ myprop”的屬性:
這是使用Archaius加載文件的方式:
ConfigurationManager.loadCascadedPropertiesFromResources("sample");String myProp = DynamicPropertyFactory.getInstance().getStringProperty("myprop", "NOT FOUND").get();assertThat(myProp, equalTo("myprop_value_default"));Archaius可以加載適合于環(huán)境的屬性,請考慮存在一個(gè)“ sample-perf.properties”,其具有針對perf環(huán)境覆蓋的相同配置:
myprop=myprop_value_perf現(xiàn)在,可以通過在sample.properties文件中添加以下內(nèi)容來指示Archaius以級聯(lián)方式加載配置:
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物業(yè)基礎(chǔ)
Spring屬性基礎(chǔ)在此處的Spring Framework參考站點(diǎn)中有很好的解釋。 簡而言之,如果有一個(gè)屬性文件“ sample.properties”,則可以通過以下方式加載和引用該文件:
@Configuration @PropertySource("classpath:/sample.properties") public class AppConfig {@AutowiredEnvironment env;@Beanpublic 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;@Beanpublic TestBean testBean() {TestBean testBean = new TestBean();testBean.setName(myProp));return testBean;}@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}}使Archaius屬性對Spring可見
因此,現(xiàn)在的問題是如何在Spring中顯示Archaius屬性,我所采用的方法是一種快速而骯臟的方法,但是可以根據(jù)需要進(jìn)行清理。 我的方法是定義一個(gè)Spring PropertySource ,在內(nèi)部將其委托給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);}}@Overridepublic Object getProperty(String name) {return DynamicPropertyFactory.getInstance().getStringProperty(name, null).get();} }棘手的部分是使用Spring注冊此新的PropertySource,這可以使用ApplicationContextInitializer來完成,該方法在初始化應(yīng)用程序上下文之前被觸發(fā):
import com.netflix.config.ConfigurationBasedDeploymentContext; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.util.StringUtils;public class SpringProfileSettingApplicationContextInitializerimplements ApplicationContextInitializer<ConfigurableApplicationContext> {@Overridepublic void initialize(ConfigurableApplicationContext ctx) {ctx.getEnvironment().getPropertySources().addFirst(new SpringArchaiusPropertySource("samples"));} }最后在這里描述了如何使用Spring注冊這個(gè)新的ApplicationContextInitializer。 本質(zhì)上就是這樣,現(xiàn)在Netflix Archaius屬性應(yīng)該可以在Spring應(yīng)用程序中工作。
翻譯自: https://www.javacodegeeks.com/2015/03/netflix-archaius-properties-in-a-spring-project.html
總結(jié)
以上是生活随笔為你收集整理的Spring项目中的Netflix Archaius属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在非托管对象中使用Spring托管Bea
- 下一篇: Byteman –用于字节码操纵的瑞士军