@value

fxnxkyjh  于 2021-07-24  发布在  Java
关注(0)|答案(4)|浏览(250)

我有一个 data.yml 在以下结构的资源文件夹中:

main:
  header:
    info: 3600L

我使用springboot2.4.2版本,我想注入属性 main.header1.info 对于字段,我按以下方式执行:

@Component
@PropertySource("classpath:data.yml")
public class SomeClass {
    @Value("`main.header1.info")
    private long info;
    ...
}

但是 NumberFormatException 眼:

java.lang.NumberFormatException: For input string: "main.header1.info"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:na]
    at java.base/java.lang.Long.parseLong(Long.java:692) ~[na:na]
    ...

我知道 long 中不支持 yml ,但我认为情况并非如此。我尝试了其他数值类型和相应的 Package 类,比如 Double . 那么,如何解决这个问题呢?

9nvpjoqh

9nvpjoqh1#

我建议使用 application.yml 文件(尽管是定制的) YAML 文件。
原因: application.properties 是spring的默认配置文件。如果您使用它,您就不必担心在spring处理时手动将文件加载到上下文中。但是,在您的情况下,您尝试从自定义yaml文件加载和读取值。所以, @PropertySource 在这里帮不上忙。
有关yaml缺点的详细信息,请参阅spring文档。
但是,如果仍希望从自定义yaml读取值,则需要编写自定义类(例如: CustomYamlPropertySourceFactory )哪个实现了 PropertySourceFactory &通知 @PropertySource 使用此工厂类。
参考代码:

@Component
    @PropertySource(value = "classpath:date.yml", factory = CustomYamlPropertySourceFactory.class) 
    public class SomeClass {

    @Value("${main.header.info}")
    private int info;
}
368yc8dk

368yc8dk2#

不能将yml文件用于 @PropertySource .
无法使用 @PropertySource 注解。因此,如果需要以这种方式加载值,则需要使用属性文件。
参见yaml文档。

rqenqsqc

rqenqsqc3#

你必须使用 ${main.header.info} 从属性中注入值。 @PropertySource 不支持加载 .yaml 或者 yml 文件夹。尝试使用 .properties 用于加载它们的文件 @PropertySource .
有一个开放的问题,但 Spring 家伙关闭了它。许多开发商对此表示反对和不满。他们仍然要求重新讨论这个问题。
阅读这里:https://github.com/spring-projects/spring-framework/issues/18486
读m。deinum文章提供了一个解决这个问题的方法,使用 @PropertySource .
而是创建 data.properties 文件。

main.header.info=3600L

将代码修改为:

@Component
@PropertySource("classpath:data.properties")
public class SomeClass {
    @Value("${main.header1.info}")
    private long info;
    ...
}
5sxhfpxr

5sxhfpxr4#

对于那些由于限制不能使用 application.properties 文件和需要作为 YAML 文件:
正如许多答案所说,spring没有默认的解析方法 YAML 属性文件。默认值 DefaultPropertySourceFactory 实现只能解析 .properties 以及 .xml 文件夹。
您需要创建一个自定义工厂类 PropertySourceFactory 让框架知道如何解析特定的配置源。
下面是一个基本的例子:

public class YamlPropertySourceFactoryExample implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(final String name, final EncodedResource resource) throws IOException {
        final Properties propertiesFromYaml = loadYamlIntoProperties(resource);
        final String sourceName = name != null ? name : resource.getResource().getFilename();
        return new PropertiesPropertySource(sourceName, propertiesFromYaml);
    }

    private Properties loadYamlIntoProperties(final EncodedResource resource) {

        final YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(resource.getResource());
        factory.afterPropertiesSet();
        return factory.getObject();
    }
}

有了它,你就完成了两件事:
加载 YAML 将配置转换为 Properties 对象。
返回 PropertiesPropertySource Package 已加载的 Properties .
之后你可以在一个 factories 任何一种的心房 @PropertyResource :

@PropertySource(value = "classpath:path/to/your/properties.yml", factory = YamlPropertySourceFactoryExample.class)

编辑:不能与一起使用 @TestPropertySource ,它不支持任何 factories 目前。

相关问题