email:
from: myemail@mydomain.com
to: theiremail@theirdomain.com@ConstructorBinding
@ConfigurationProperties(prefix = "email")
data class EmailProperties(
val from: String,
val to: String
)The data class has to be annoted with two annotations:
@ConfigurationPropertiesto bind the properties in ourpropertiesorymlfile to this class.@ConstructorBindingbecause we're using a data class. By default, Spring uses the default constructor and setters to instantiate the class annotated by@ConfigurationProperties, this obviously doesn't work with data classes.
We need to add some configuration to an existing or new class annotated with @Configuration. In this example, we will use a new class.
@Configuration
@ConfigurationPropertiesScan
class EmailConfigurationWe need only a single annotation: @ConfigurationPropertiesScan. This will enable scanning for any @ConfigurationProperties annotated classes in the current package and any sub-packages.