Add support for DataSize

This commit adds support for Spring Framework's `DataSize` allowing to
express a size in bytes and other convenient units.

Similar to the `Duration` support introduced previously, this commit
adds transparent binding support as well as detection of default values
in `@ConfigurationProperties`-annotated object.

Closes gh-13974
This commit is contained in:
Stephane Nicoll
2018-08-10 14:58:11 +02:00
parent 78dd7bd934
commit 94013aaba6
11 changed files with 478 additions and 8 deletions

View File

@@ -1308,6 +1308,45 @@ while supporting a much richer format.
[[boot-features-external-config-conversion-datasize]]
===== Converting Data Sizes
Spring Framework has a `DataSize` value type that allows to express 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)
Consider the following example:
[source,java,indent=0]
----
include::{code-examples}/context/properties/bind/AppIoProperties.java[tag=example]
----
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 unit. 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.
TIP: If you are upgrading from a previous version that is simply using `Long` to express
the size, make sure to define the unit (using `@DataSizeUnit`) if it isn't bytes alongside
the switch to `DataSize`. Doing so gives a transparent upgrade path while supporting a
much richer format.
[[boot-features-external-config-validation]]
==== @ConfigurationProperties Validation
Spring Boot attempts to validate `@ConfigurationProperties` classes whenever they are

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docs.context.properties.bind;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
/**
* A {@link ConfigurationProperties} example that uses {@link DataSize}.
*
* @author Stephane Nicoll
*/
// tag::example[]
@ConfigurationProperties("app.io")
public class AppIoProperties {
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize bufferSize = DataSize.ofMegaBytes(2);
private DataSize sizeThreshold = DataSize.ofBytes(512);
public DataSize getBufferSize() {
return this.bufferSize;
}
public void setBufferSize(DataSize bufferSize) {
this.bufferSize = bufferSize;
}
public DataSize getSizeThreshold() {
return this.sizeThreshold;
}
public void setSizeThreshold(DataSize sizeThreshold) {
this.sizeThreshold = sizeThreshold;
}
}
// end::example[]