java - Spring Boot 2属性配置

标签 java spring spring-boot

我有一些代码可以在 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 个参数。

(响应也不相同,当我尝试调用新方法时,响应对象被包装而不是直接给我字符串/整数等...)

最佳答案

PropertiesConfigurationFactory应替换为 Binder类。

Binder class

示例代码:-

ConfigurationPropertySource source = new MapConfigurationPropertySource(
                loadProperties(resource));
Binder binder = new Binder(source);
return binder.bind("initializr", InitializrProperties.class).get();

We were also using PropertiesConfigurationFactory to bind a POJO to a prefix of the Environment. In 2.0, a brand new Binder API was introduced that is more flexible and easier to use. Our binding that took 10 lines of code could be reduced to 3 simple lines.

YamlPropertySourceLoader:-

是的,这个类在版本2中已经改变了。它不接受第三个参数profile不再了。方法签名已更改为返回 List<PropertySource<?>>而不是PropertySource<?> 。如果您期望单一来源,请从列表中获取第一个出现的位置。

Load the resource into one or more property sources. Implementations may either return a list containing a single source, or in the case of a multi-document format such as yaml a source for each document in the resource.

关于java - Spring Boot 2属性配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52421180/

相关文章:

javascript - Spring项目中在新窗口中打开jsp

spring - 使用Spring Boot将实现(例如Hibernate Validator)添加到类路径错误中

java - 通过我的应用程序删除驻留在内部 Linux 服务器上的文件

java - Spring Boot 启动后运行代码

java - SpringInputGeneralFieldTagProcessor 中的 Spring Thymeleaf TemplateProcessingException

java - 根据内容检测文件类型

java - Junit-快速检查 : Generate String matching a pattern

Java - 如何在运行 readLine 命令之前检查 BufferedReader 是否包含换行符

Java - 使用 hasNext() 方法时出现 "cannot find symbol"错误

java - 循环时获取 "Neither BindingResult nor plain target object for bean name ' bean name' 作为请求属性可用