[英]How to auto convert string values from properties files to other data types i.e. duration using spring conversion service?
How to auto convert string values from properties files to other data types ie duration using spring conversion service? 如何使用Spring转换服务将字符串值从属性文件自动转换为其他数据类型,即持续时间?
properties file 属性文件
my.duration=PT10M
configuration class 配置类
@Value("{my.duration}")
//It'll give string value by default but I want to convert to duration
private Duration myDuration;
How can I achieve that using spring ConversionService
? 如何使用spring
ConversionService
实现该目标?
Here you can use YAML, if you want to populate a class with the values from resource files. 如果要使用资源文件中的值填充类,则可以在此处使用YAML。
Duration.yaml 持续时间
duration:
val1: "PT"
val2: "10"
val3: "M"
Duration.java 持续时间
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "duration")
public class Duration {
private String val1;
private String val2;
private String val3;
// setters and getters.
}
You can play around with this approach to get the desired output. 您可以使用这种方法来获得所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.