Parsing @ConfigurationProperties annotations in Spring Boot
In the Spring Boot framework, configuration management is a core feature.
Spring Boot provides a variety of ways to handle external configurations, among which@ConfigurationProperties
Annotation is a very powerful and flexible tool.
This article will discuss in depth@ConfigurationProperties
The concept, usage, working principle, configuration binding, type safety and how to apply it in actual development.
What is @ConfigurationProperties?
@ConfigurationProperties
Is annotation provided by Spring Boot, which is used to bind external configuration properties to Java objects.
By using this annotation, developers can add configuration files (e.g.or
) attribute values in ) are automatically mapped to fields in Java classes, thus achieving centralized management of configurations and type safety.
@ConfigurationProperties' role
- Configure binding: Bind the attribute values in the configuration file to fields of the Java class to implement automatic mapping of configuration.
- Type safety: Provide type-safe configuration binding to avoid type conversion errors.
- Complex configuration: Supports binding of complex configuration structures, such as nested objects, collections, maps, etc.
-
Configuration verification: Combined
@Valid
Annotation to realize the verification of configuration properties.
Basic usage of @ConfigurationProperties
1. Define the configuration class
First, define a Java class to bind configuration properties.
use@ConfigurationProperties
Annotation marks the class and specifies a prefix.
Sample code:
import ; import ; @Component @ConfigurationProperties(prefix = "app") public class AppProperties { private String name; private String version; private boolean enabled; // getters and setters }
explain:
-
@ConfigurationProperties(prefix = "app")
: Specify the prefix of the configuration property asapp
。 -
@Component
: Register this class as a Spring Bean so that it can be managed by Spring containers.
2. Configuration file
existor
Define configuration properties in the file.
Sample code():
=MyApp =1.0.0 =true
Sample code():
app: name: MyApp version: 1.0.0 enabled: true
explain:
- Configure properties to
app
prefixed with@ConfigurationProperties
The prefixes in the annotation are consistent.
3. Enable configuration property support
On the main class or configuration class of the Spring Boot application, use@EnableConfigurationProperties
Annotation enables configuration property support.
Sample code:
import ; import ; import ; @SpringBootApplication @EnableConfigurationProperties() public class MyAppApplication { public static void main(String[] args) { (, args); } }
explain:
-
@EnableConfigurationProperties()
:EnableAppProperties
Class configuration property binding supports.
Advanced usage of @ConfigurationProperties
1. Nested object binding
@ConfigurationProperties
Supports binding of nested objects, and can realize mapping of complex configuration structures.
Sample code:
import ; import ; @Component @ConfigurationProperties(prefix = "app") public class AppProperties { private String name; private String version; private boolean enabled; private Server server; // getters and setters public static class Server { private String host; private int port; // getters and setters } }
Configuration file():
=MyApp =1.0.0 =true =localhost =8080
Configuration file():
app: name: MyApp version: 1.0.0 enabled: true server: host: localhost port: 8080
explain:
- Nested objects
Server
The properties ofPrefix for binding.
2. Collection and Map binding
@ConfigurationProperties
Supports collection and Map type binding, which can achieve a more flexible configuration structure.
Sample code:
import ; import ; import ; import ; @Component @ConfigurationProperties(prefix = "app") public class AppProperties { private String name; private String version; private boolean enabled; private List<String> features; private Map<String, String> settings; // getters and setters }
Configuration file():
=MyApp =1.0.0 =true [0]=feature1 [1]=feature2 .key1=value1 .key2=value2
Configuration file():
app: name: MyApp version: 1.0.0 enabled: true features: - feature1 - feature2 settings: key1: value1 key2: value2
explain:
- gather
features
and Mapsettings
The properties ofand
Prefix for binding.
3. Configuration verification
Combined@Valid
Annotation can realize the verification of configuration properties to ensure the validity of configuration.
Sample code:
import ; import ; import ; import ; import ; @Component @ConfigurationProperties(prefix = "app") @Validated public class AppProperties { @NotEmpty private String name; @NotNull private String version; private boolean enabled; // getters and setters }
explain:
-
@Validated
: Enable verification support. -
@NotEmpty
and@NotNull
:rightname
andversion
The field is checked non-empty.
How @ConfigurationProperties works
@ConfigurationProperties
The working principle of the annotation mainly involves the following steps:
-
Attribute scanning: When Spring Boot application starts, all with
@ConfigurationProperties
Annotated class. - Attribute binding: Bind the property values in the configuration file to the field of the class based on the prefix specified in the annotation.
- Type conversion: Spring Boot has built-in multiple type converters, which can convert configuration property values to corresponding Java types.
-
check: Combined
@Valid
Annotation, check the bound configuration properties.
@ConfigurationProperties Best Practices
- Rationally divide configuration categories: Rationally divide configuration classes according to functional modules to avoid the large size of a single configuration class.
- Using nested objects: For complex configuration structures, nested objects are used for binding to improve the readability and maintainability of the configuration.
-
Configuration verification: Combined
@Valid
Annotation, verify the configuration properties to ensure the validity of the configuration. - Documentation and comments: Add documents and comments to the configuration class to explain the role and value range of configuration properties, so that team members can understand and maintain them.
in conclusion
@ConfigurationProperties
Is a very powerful and flexible tool in Spring Boot for binding external configuration properties to Java objects.
By using this annotation, developers can achieve centralized management of configurations and type safety, improving development efficiency and code quality.
I hope that through this article, you will understand the Spring Boot@ConfigurationProperties
Annotations have a deeper understanding and can be flexibly applied in actual development.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.