javascript
Spring项目中的Netflix Archaius属性
Archaius基礎
Netflix Archaius是用于管理應用程序配置的庫。 考慮一個屬性文件“ sample.properties”,其中包含一個名為“ myprop”的屬性:
這是使用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 {@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可見
因此,現在的問題是如何在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);}}@Overridepublic 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 SpringProfileSettingApplicationContextInitializerimplements ApplicationContextInitializer<ConfigurableApplicationContext> {@Overridepublic 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
總結
以上是生活随笔為你收集整理的Spring项目中的Netflix Archaius属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在非托管对象中使用Spring托管Bea
- 下一篇: Byteman –用于字节码操纵的瑞士军