用scala做网站,百度投放广告流程,怎么更改自动目录的格式,有没有免费资源原文网址#xff1a;SpringBoot--yml配置文件的时间/大小的单位转换_IT利刃出鞘的博客-CSDN博客
简介
说明
本文介绍SpringBoot的yml#xff08;properties#xff09;配置文件的时间/大小的单位转换。
概述
SpringBoot可以将yml中的配置绑定到一个Java类的字段#x…原文网址SpringBoot--yml配置文件的时间/大小的单位转换_IT利刃出鞘的博客-CSDN博客
简介
说明
本文介绍SpringBoot的ymlproperties配置文件的时间/大小的单位转换。
概述
SpringBoot可以将yml中的配置绑定到一个Java类的字段而且支持单位的转换。以时间为例yml中指定为2m则可以用Duration来接收这个字段接收到的字段值为3分钟。
注意
本处的单位转换支持配置放到一个类中也支持Value等。
时间的转换
概述
Spring 使用 java.time.Duration 类代表时间大小以下场景适用
除非指定 DurationUnit 否则一个 long 代表的时间为毫秒。ISO-8601 标准格式 java.time.Duration 的实现就是参照此标准你也可以使用以下支持的单位用大写也可以 ns - 纳秒us - 微秒ms - 毫秒s - 秒m - 分h - 时d - 天
示例
application.yml
custom:monitor:name: myMonitorinterval: 3m
实体类
package com.knife.config;import lombok.Data;import java.time.Duration;Data
public class MonitorProperty {private String name;private Duration interval;
}配置类
package com.knife.config;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MonitorConfig {ConfigurationProperties(prefix custom.monitor)Beanpublic MonitorProperty monitorProperty() {return new MonitorProperty();}
}测试类
package com.knife.controller;import com.knife.config.MonitorProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;RestController
public class HelloController {Autowiredprivate MonitorProperty monitorProperty;GetMapping(/test)public String test() {return test success;}
}测试
打个断点然后请求 工具类实例
SpringBoot的转换时间的工具类是DurationStyleorg.springframework.core.convert.support包。
示例
import org.springframework.core.convert.support.DurationStyle;
import java.time.Duration;public class MyApp {public static void main(String[] args) {String durationString 3m;Duration duration DurationStyle.SIMPLE.parse(durationString);System.out.println(duration); // 输出 PT3M (3 minutes)}
}大小的转换
上边是文章部分内容为便于维护全文已转移到此网址SpringBoot-yml配置文件的时间/大小的单位转换 - 自学精灵