SoFunction
Updated on 2025-03-03

Interpretation of @ConfigurationProperties annotation in Spring Boot

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@ConfigurationPropertiesAnnotation is a very powerful and flexible tool.

This article will discuss in depth@ConfigurationPropertiesThe concept, usage, working principle, configuration binding, type safety and how to apply it in actual development.

What is @ConfigurationProperties?

@ConfigurationPropertiesIs 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

  1. Configure binding: Bind the attribute values ​​in the configuration file to fields of the Java class to implement automatic mapping of configuration.
  2. Type safety: Provide type-safe configuration binding to avoid type conversion errors.
  3. Complex configuration: Supports binding of complex configuration structures, such as nested objects, collections, maps, etc.
  4. Configuration verification: Combined@ValidAnnotation 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@ConfigurationPropertiesAnnotation 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

existorDefine 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 toappprefixed with@ConfigurationPropertiesThe prefixes in the annotation are consistent.

3. Enable configuration property support

On the main class or configuration class of the Spring Boot application, use@EnableConfigurationPropertiesAnnotation enables configuration property support.

Sample code:

import ;
import ;
import ;

@SpringBootApplication
@EnableConfigurationProperties()
public class MyAppApplication {

    public static void main(String[] args) {
        (, args);
    }
}

explain:

  • @EnableConfigurationProperties():EnableAppPropertiesClass configuration property binding supports.

Advanced usage of @ConfigurationProperties

1. Nested object binding

@ConfigurationPropertiesSupports 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 objectsServerThe properties ofPrefix for binding.

2. Collection and Map binding

@ConfigurationPropertiesSupports 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:

  • gatherfeaturesand MapsettingsThe properties ofandPrefix for binding.

3. Configuration verification

Combined@ValidAnnotation 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.
  • @NotEmptyand@NotNull:rightnameandversionThe field is checked non-empty.

How @ConfigurationProperties works

@ConfigurationPropertiesThe working principle of the annotation mainly involves the following steps:

  1. Attribute scanning: When Spring Boot application starts, all with@ConfigurationPropertiesAnnotated class.
  2. Attribute binding: Bind the property values ​​in the configuration file to the field of the class based on the prefix specified in the annotation.
  3. Type conversion: Spring Boot has built-in multiple type converters, which can convert configuration property values ​​to corresponding Java types.
  4. check: Combined@ValidAnnotation, check the bound configuration properties.

@ConfigurationProperties Best Practices

  1. Rationally divide configuration categories: Rationally divide configuration classes according to functional modules to avoid the large size of a single configuration class.
  2. Using nested objects: For complex configuration structures, nested objects are used for binding to improve the readability and maintainability of the configuration.
  3. Configuration verification: Combined@ValidAnnotation, verify the configuration properties to ensure the validity of the configuration.
  4. 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

@ConfigurationPropertiesIs 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@ConfigurationPropertiesAnnotations 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.