亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

spring boot 2 屬性配置

spring boot 2 屬性配置

慕森王 2021-09-29 16:21:40
我有一些代碼可以在 2 之前的 spring boot 上正常工作,我發現很難將其轉換為與 spring boot 2 一起使用。有人可以幫忙嗎?public static MutablePropertySources buildPropertySources(String propertyFile, String profile){    try    {        Properties properties = new Properties();        YamlPropertySourceLoader loader = new YamlPropertySourceLoader();        // load common properties        PropertySource<?> applicationYamlPropertySource = loader.load("properties", new ClassPathResource(propertyFile), null);        Map<String, Object> source = ((MapPropertySource) applicationYamlPropertySource).getSource();        properties.putAll(source);        // load profile properties        if (null != profile)        {            applicationYamlPropertySource = loader.load("properties", new ClassPathResource(propertyFile), profile);            if (null != applicationYamlPropertySource)            {                source = ((MapPropertySource) applicationYamlPropertySource).getSource();                properties.putAll(source);            }        }        propertySources = new MutablePropertySources();        propertySources.addLast(new PropertiesPropertySource("apis", properties));    }    catch (Exception e)    {        log.error("{} file cannot be found.", propertyFile);        return null;    }}public static <T> void handleConfigurationProperties(T bean, MutablePropertySources propertySources) throws BindException{    ConfigurationProperties configurationProperties = bean.getClass().getAnnotation(ConfigurationProperties.class);    if (null != configurationProperties && null != propertySources)    {        String prefix = configurationProperties.prefix();        String value = configurationProperties.value();        if (null == value || value.isEmpty())        {            value = prefix;        }PropertiesConfigurationFactory不再存在并且YamlPropertySourceLoaderload 方法不再接受 3 個參數。(響應也不相同,當我嘗試調用新方法時,響應對象被包裝而不是給我直接字符串/整數等......)
查看完整描述

3 回答

?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

該PropertiesConfigurationFactory應改為Binder類。


粘合劑類


示例代碼:-


ConfigurationPropertySource source = new MapConfigurationPropertySource(

                loadProperties(resource));

Binder binder = new Binder(source);

return binder.bind("initializr", InitializrProperties.class).get();

我們還使用 PropertiesConfigurationFactory 將 POJO 綁定到 Environment 的前綴。在 2.0 中,引入了全新的 Binder API,更加靈活易用。我們需要 10 行代碼的綁定可以減少到 3 行簡單的代碼。


YamlPropertySourceLoader:-


是的,這個類在版本 2 中已經改變了。它不再接受第三個參數profile。方法簽名已更改為 returnList<PropertySource<?>>而不是PropertySource<?>。如果您期待單一來源,請從列表中獲取第一個。


將資源加載到一個或多個屬性源中。實現可以返回包含單個源的列表,或者在多文檔格式(例如 yaml)的情況下返回資源中每個文檔的源。


查看完整回答
反對 回復 2021-09-29
  • 3 回答
  • 0 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號