According to this documentation, I should be able to use @DurationUnit to change the default unit when converting a duration. However, the example above keeps using MILLISECONDS as the duration unit.
Happening in the latest Spring version
1) When not specifying a unit in application.yml:
myconfig:
myDuration: 70
2) And specifying DurationUnit to SECONDS in config properties class
@ConstructorBinding
@ConfigurationProperties("myconfig")
class MyProperties(
@DurationUnit(ChronoUnit.SECONDS)
val myDuration: Duration,
)
3) Then duration is converted using default MILLISECONDS unit
myProperties.myDuration.toString() // -> "PT0.07S"
Comment From: jordigarcl
Also, I don't understand why the current documentation provides this example:
@ConfigurationProperties("app.system")
public class AppSystemProperties {
@DurationUnit(ChronoUnit.SECONDS)
private Duration sessionTimeout = Duration.ofSeconds(30);
}
Why is it programmatically setting Duration.ofSeconds(30) ? Isn't the whole purpose to use the properties value?
Comment From: wilkinsona
Duration.ofSeconds(30) is the default value for the app.system.sessionTimeout property.
Comment From: wilkinsona
This has been addressed in 2.4 where @DurationUnit can now be used on constructor parameters. I think we still need to do something in a maintenance branch, though. The problem isn't specific to Kotlin as the following Java does not work either:
@DurationUnit(ChronoUnit.SECONDS)
private final Duration duration;
public MyProperties(Duration duration) {
this.duration = duration;
}
We can't be sure that the duration constructor parameter will be stored in the duration field so there's no way of reliably discovering the duration unit. I suspect the best that we can do is to update the documentation to make it clear that @DurationUnit is only supported with JavaBean-based property binding.