email:
from: [email protected]
to: [email protected]
@ConstructorBinding
@ConfigurationProperties(prefix = "email")
data class EmailProperties(
val from: String,
val to: String
)
The data class has to be annoted with two annotations:
@ConfigurationProperties
to bind the properties in ourproperties
oryml
file to this class.@ConstructorBinding
because 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 EmailConfiguration
We need only a single annotation: @ConfigurationPropertiesScan
. This will enable scanning for any @ConfigurationProperties
annotated classes in the current package and any sub-packages.