What is @ConfigurationProperties annotation in spring boot?
Spring Boot allows you to externalize your configuration so you can work with the same application code
in different environments. You can use properties files, YAML files, environment variables and command-line arguments
to externalize configuration. Property values can be injected directly into your beans using the @Value annotation,
accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties.
Type-safe configuration properties
Using the @Value("${property.name}") annotation to inject configuration properties can sometimes be cumbersome,
especially if you are working with multiple properties or your data is hierarchical in nature. Spring Boot provides an
alternative method of working with properties that allows strongly typed beans to govern and validate the configuration
of your application.
Relaxed binding
Spring Boot automatically uses relaxed rules for binding Environment properties to @ConfigurationProperties
beans. Meaning, there doesn’t need to be an exact match between the Environment property name and the bean property name.
Imagine you have a bean property called allowCredentials you can use the following configuration in your properties.
- allowCredentials: using standard camel case syntax.
- allow-credentials: using dashed notation.
- allow_credentials: using underscore notation.
- ALLOW_CREDENTIALS: using upper case format.
@ConfigurationProperties example is in the next page.
|