日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring项目中的Netflix Archaius属性

發布時間:2023/12/3 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 {@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属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。