总结:图
Duration.parse()只接受的格式基于ISO-8601持续时间格式PnDTnHnMn.nS
springboot@ConfigurationProperties支持更多的格式
@Data
@Component
@ConfigurationProperties(prefix = "my")
public class MyProperties {
Duration nanos;
Duration millis;
Duration seconds;
Duration minutes;
Duration hour;
Duration day;
}
@Slf4j
@RestController
public class MyController {
@Autowired
private MyProperties myProps;
@GetMapping("prop")
void prop() {
// 接受的格式基于ISO-8601持续时间格式PnDTnHnMn.nS ,其中几天被认为是24小时。
log.info("{}",myProps);
log.info("{}",myProps.nanos.toNanos());
// log.info("{}",Duration.parse("PT1ns")); // 不支持
log.info("{}",myProps.millis.toNanos());
// log.info("{}",Duration.parse("PT1ms")); // 不支持
log.info("{}",myProps.seconds.toNanos());
log.info("{}",Duration.parse("PT1.0s").toNanos() + "\t\t\t" + Duration.parse("PT1.0s"));
log.info("{}",myProps.minutes.toNanos());
log.info("{}",Duration.parse("PT1m").toNanos() + "\t\t\t" + Duration.parse("PT1m"));
log.info("{}",myProps.hour.toNanos());
log.info("{}",Duration.parse("PT1h").toNanos() + "\t\t\t" + Duration.parse("PT1h"));
log.info("{}",myProps.day.toNanos());
log.info("{}",Duration.parse("P1D").toNanos() + "\t\t\t" + Duration.parse("P1D"));
}
}
# application.yml
my:
nanos: 1ns
millis: 1ms
seconds: 1s
minutes: 1m
hour: 1h
day: 1d