@ConfigurationProperties

The @ConfigurationProperties annotation is used in Spring Boot to bind external application properties to a class or bean. This allows developers to easily access and use the properties defined in the application’s configuration files (such as application.properties or application.yml) within their code.

When the annotation is used, Spring Boot will automatically create an instance of the class and bind the properties from the configuration files to the class’ fields. The properties in the configuration files should have a prefix that matches the class name, and the fields in the class should have matching names.

Here’s an example of how you might use the @ConfigurationProperties annotation in a Spring Boot application:

@Configuration
@ConfigurationProperties(prefix = "app")
public class AppProperties {

    private String name;
    private String version;
    private List<String> features;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
    
    public List<String> getFeatures() {
        return features;
    }
    
    public void setFeatures(List<String> features) {
        this.features = features;
    }
}

In this example, we have a class called AppProperties which has fields name, version and features. The @ConfigurationProperties annotation is used on this class, with the prefix attribute set to "app". This tells Spring Boot to bind properties from the configuration files that start with “app” to the fields in this class.

You could then have a properties file called application.properties or application.yml with the following properties defined:

app.name=MyApp
app.version=1.0
app.features[0]=feature1
app.features[1]=feature2

You can then use the AppProperties class in other parts of your code to access the properties defined in the configuration file:

@Autowired
AppProperties appProperties;

...

String appName = appProperties.getName();
String appVersion = appProperties.getVersion();
List<String> appFeatures = appProperties.getFeatures();

Spring Boot provides support for validation of properties when using the @ConfigurationProperties annotation. You can use JSR-303 Bean Validation annotations such as @NotNull, @Size, @Min, @Max, etc. to validate the properties.

Here’s an example of how you might use validation with @ConfigurationProperties:


@Configuration
@Validated
@ConfigurationProperties(prefix = "app")
public class AppProperties {

    @NotNull
    private String name;

    @Min(1)
    @Max(10)
    private int maxUsers;

    @Size(min = 2, max = 20)
    private String adminUser;

    // getters and setters
}

It’s important to note that you will also need to add a javax.validation.Validator bean to your Spring configuration in order to enable validation. You can use @EnableConfigurationProperties annotation on the class level to automatically register the bean.

@EnableConfigurationProperties(AppProperties.class)
@Configuration
public class AppConfig {
  // ...
}

Alternatively, you can also use spring-boot-starter-validation dependency which will automatically create the Validator bean.

With this setup, Spring Boot will automatically validate the properties when they are set, and throw a ConstraintViolationException if any of the properties fail validation.

Leave a Reply

Your email address will not be published. Required fields are marked *