Recently, in a project that is using spring-framework (no spring-boot), I saw a @Configuration class with a lot of configuration about timeouts injected as plain long (most likely milliseconds but it is not explicitly documented, as often it happens):

@Value("${connect.timeout}")
long connectTimeout;

With this small patch, it would be possible to directly inject a Duration:

@Value("${connect.timeout}")
Duration connectTimeout;

In my opinion, this is less error prone, as usually these configuration values must be adjusted with some calculations (i.e. seconds to milliseconds).

This patch allows two formats: - ISO 8601 format with PT prefix, e.g. PT10s, PT1m30s (as Duration.parse); - simplified format without PT prefix, e.g. 10s, 1m, 2h (IMHO this format is very readable for properties files).

Comments and feedback is welcome. I'm not sure about using a property editor, perhaps a Converter is a better choice? And I'm not sure if this should be mentioned somewhere in the spring-reference... please advice :-)

Comment From: pivotal-cla

@dfa1 Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

Comment From: pivotal-cla

@dfa1 Thank you for signing the Contributor License Agreement!

Comment From: pivotal-cla

@dfa1 Thank you for signing the Contributor License Agreement!

Comment From: dfa1

@rstoyanchev just reworked a bit the commit, can you please have a look?

Comment From: snicoll

@dfa1 thaks for the PR and sorry for the delay. We've been working on an extensive review of duration support in #30396. I unfortunately am going to close this PR in favor of ours.

Comment From: dfa1

@snicoll no worries... the new PR is much better and complete! 😇

thanks!