Java @Spring Boot中yaml文件中数字类型的值
我在以下结构的参考资料文件夹中获得了data.yml:Java @Spring Boot中yaml文件中数字类型的值,java,spring,spring-boot,yaml,Java,Spring,Spring Boot,Yaml,我在以下结构的参考资料文件夹中获得了data.yml: main: header: info: 3600L 我使用的是Spring Boot版本2.4.2,我想将属性main.header1.info注入一个字段,我采用以下方法: @Component @PropertySource("classpath:data.yml") public class SomeClass { @Value("`main.header1.info")
main:
header:
info: 3600L
我使用的是Spring Boot版本2.4.2,我想将属性main.header1.info注入一个字段,我采用以下方法:
@Component
@PropertySource("classpath:data.yml")
public class SomeClass {
@Value("`main.header1.info")
private long info;
...
}
但是有一个数字形式的视觉感受:
我知道yml不支持long,但我认为情况并非如此。我尝试了其他数值类型和相应的包装器类,比如Double。
那么,如何解决这个问题呢?您必须使用${main.header.info}从属性中注入值。
@PropertySource不支持加载.yaml或yml文件。
尝试使用.properties文件来使用@PropertySource加载它们
有一个问题是开放的,但春天的家伙关闭了它。许多的
开发商对此表示反对,并表示不满。他们仍然要求
重新打开这个问题
请看这里:
请阅读此问题的解决方法,以使用@PropertySource加载YML文件
而是创建data.properties文件
将代码修改为:
@Component
@PropertySource("classpath:data.properties")
public class SomeClass {
@Value("${main.header1.info}")
private long info;
...
}
必须使用${main.header.info}从属性中注入值。
@PropertySource不支持加载.yaml或yml文件。
尝试使用.properties文件来使用@PropertySource加载它们
有一个问题是开放的,但春天的家伙关闭了它。许多的
开发商对此表示反对,并表示不满。他们仍然要求
重新打开这个问题
请看这里:
请阅读此问题的解决方法,以使用@PropertySource加载YML文件
而是创建data.properties文件
将代码修改为:
@Component
@PropertySource("classpath:data.properties")
public class SomeClass {
@Value("${main.header1.info}")
private long info;
...
}
不能将yml文件与@PropertySource一起使用 无法使用@PropertySource注释加载YAML文件。因此,如果需要以这种方式加载值,则需要使用属性文件
请参阅文档。您不能将yml文件与@PropertySource一起使用 无法使用@PropertySource注释加载YAML文件。因此,如果需要以这种方式加载值,则需要使用属性文件 请参阅文档。我建议使用application.yml文件,而不要使用自定义YAML文件 原因:application.properties是spring的默认配置文件。如果您使用它,就不必担心将文件加载到 在spring处理上下文时手动创建上下文。但是,在你的情况下,你是 尝试从自定义YAML文件加载和读取值。所以 @PropertySource在这里帮不上忙 有关YAML缺点的详细信息,请参阅 但是,如果仍希望从自定义yaml读取值,则需要编写一个自定义类,例如:CustomYamlPropertySourceFactory,它实现PropertySourceFactory并通知@PropertySource使用此工厂类 参考代码: 我建议使用application.yml文件,而不是自定义YAML文件 原因:application.properties是spring的默认配置文件。如果您使用它,就不必担心将文件加载到 在spring处理上下文时手动创建上下文。但是,在你的情况下,你是 尝试从自定义YAML文件加载和读取值。所以 @PropertySource在这里帮不上忙 有关YAML缺点的详细信息,请参阅 但是,如果仍希望从自定义yaml读取值,则需要编写一个自定义类,例如:CustomYamlPropertySourceFactory,它实现PropertySourceFactory并通知@PropertySource使用此工厂类 参考代码: 对于那些由于限制无法使用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();
}
}
编辑:它不能与@TestPropertySource一起使用,它目前不支持任何工厂。对于那些由于限制无法使用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();
}
}
编辑:它不能与@TestPropertySource一起使用,目前它不支持任何工厂。请尝试@Value${main.header.info}。此外,如果使用Boot,则应使用@ConfigurationProperties。@Value${…}不起作用。问题是@PropertySource。。。未按预期的方式工作try@Value$
{main.header.info}。另外,如果使用Boot,则应该使用@ConfigurationProperties。@Value${…}不起作用。问题是@PropertySource。。。没有预期的效果。谢谢知道了。谢谢
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();
}
}
@PropertySource(value = "classpath:path/to/your/properties.yml", factory = YamlPropertySourceFactoryExample.class)