簡體   English   中英

Spring Boot 2屬性配置

[英]spring boot 2 properties configuration

我有一些代碼可以在Spring Boot 2之前的版本上正常工作,並且很難轉換為與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<?> configurationFactory = new PropertiesConfigurationFactory<>(bean);
        configurationFactory.setPropertySources(propertySources);
        configurationFactory.setTargetName(value);
        configurationFactory.bindPropertiesToTarget();
    }
}

PropertiesConfigurationFactory不再存在,並且YamlPropertySourceLoader加載方法不再接受3個參數。

(響應也不相同,當我嘗試調用新方法時,響應對象被包裝而不是直接給我直接的字符串/整數等。)

應該用Binder類替換PropertiesConfigurationFactory

活頁夾類

示例代碼:-

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,該API更靈活,更易於使用。 我們使用10行代碼的綁定可以簡化為3行。

YamlPropertySourceLoader:-

是的,該類在版本2中已更改。它不再接受第三個參數profile 方法簽名已更改為返回List<PropertySource<?>>而不是PropertySource<?> 如果您希望使用單一來源,請從列表中獲取第一個匹配項。

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

由於尚無可接受的答案,因此我發布了完整的解決方案,該解決方案基於@nationquest的答案:

private ConfigClass loadConfiguration(String path){
    MutablePropertySources sources = new MutablePropertySources();
    Resource res = new FileSystemResource(path);
    PropertiesFactoryBean propFactory = new PropertiesFactoryBean();
    propFactory.setLocation(res);
    propFactory.setSingleton(false);

    // resolve potential references to local environment variables
    Properties properties = null;
    try {
        properties = propFactory.getObject();
        for(String p : properties.stringPropertyNames()){
            properties.setProperty(p, env.resolvePlaceholders(properties.getProperty(p)));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    sources.addLast(new PropertiesPropertySource("prefix", properties));
    ConfigurationPropertySource propertySource = new MapConfigurationPropertySource(properties);
    return new Binder(propertySource).bind("prefix", ConfigClass.class).get();
}

最后三行是此處的相關部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM