gpt4 book ai didi

java - Spring Boot 2属性配置

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:28 26 4
gpt4 key购买 nike

我有一些代码可以在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com