Try   HackMD

Properties conversion in Spring Boot

tags: spring

Converting durations

To specify a session timeout of 30 seconds, 30, PT30S and 30s are all equivalent. A read timeout of 500ms can be specified in any of the following form: 500, PT0.5S and 500ms.

You can also use any of the supported units. These are:

ns for nanoseconds
us for microseconds
ms for milliseconds
s for seconds
m for minutes
h for hours
d for days

The default unit is milliseconds and can be overridden using @DurationUnit as illustrated in the sample above.

Reference

Converting periods

In addition to durations, Spring Boot can also work with java.time.Period type. The following formats can be used in application properties:

An regular int representation (using days as the default unit unless a @PeriodUnit has been specified).

The standard ISO-8601 format used by java.time.Period.

A simpler format where the value and the unit pairs are coupled (e.g. 1y3d means 1 year and 3 days).

The following units are supported with the simple format:

  • y for years
  • m for months
  • w for weeks
  • d for days

Reference

Converting Data Sizes

Spring Framework has a DataSize value type that expresses a size in bytes. If you expose a DataSize property, the following formats in application properties are available:

A regular long representation (using bytes as the default unit unless a @DataSizeUnit has been specified)

A more readable format where the value and the unit are coupled (e.g. 10MB means 10 megabytes).

To specify a buffer size of 10 megabytes, 10 and 10MB are equivalent. A size threshold of 256 bytes can be specified as 256 or 256B.

You can also use any of the supported units. These are:

  • B for bytes
  • KB for kilobytes
  • MB for megabytes
  • GB for gigabytes
  • TB for terabytes

The default unit is bytes and can be overridden using @DataSizeUnit as illustrated in the sample above.

Reference