Alex Zhulin
Alex Zhulin

Reputation: 1305

Spring boot. The profit of using @ConfigurationProperties annotation

Could you explain me the profit of using @ConfigurationProperties annotation.

I have to options of my code.

First with @ConfigurationProperties annotation:

@Service
@ConfigurationProperties(prefix="myApp")
public class myService() {
  private int myProperty;
  public vois setMyProperty(int myProperty) {
    this.myProperty = myProperty;
  }
  // And this I can use myProperty which was injected from application.properties (myApp.myProperty = 10)
}

The second one without @ConfigurationProperties annotation.

It looks like this:

@Service
public class myService() {
    private final Environment environment;
    public myService(Environment environment) {
        this.environment = environment;
    }
  // And this I can use myProperty which was injected from application.properties (myApp.myProperty = 10)
  environment.getProperty("myApp.myProperty")
}

With one property the amount of code looks the same, but if I have about 10 properties, the first options will have more boilerplate of code (define 10 properties and 10 setters for this properties).

The second options will have the one injection of Environment and no addition boilerplate code.

Upvotes: 2

Views: 533

Answers (3)

isank-a
isank-a

Reputation: 1675

When using ConfigurationProperties you don't need to remember the property name to retrieve it. Spring takes care of the mapping. For eg:

app.properties.username-max-length in properties

will be mapped to

String usernameMaxLength in POJO

You just need to get usernameMaxLength field name right, once.

Using ConfigurationProperties makes your code easy to test. You can have multiple properties file for different test scenarios and can use them in your tests with TestPropertySource("path").

Now, if boilerplate getters/setters bothers you. You can always use lombok's @Getter/@Setter. This is just a suggestion and depends on an individual's choice.

Also, starting from Spring Boot 2.2.0, your @ConfigurationProperties classes can be immutable

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

    private final Integer usernameMaxLength;

    @ConstructorBinding
    public AppProperties(Integer usernameMaxLength) {

        this.usernameMaxLength = usernameMaxLength;
    }

    // Getters
}

Upvotes: 0

Gaurav Dhiman
Gaurav Dhiman

Reputation: 1023

@ConfigurationProperties is annotation for externalized configuration. To inject property value from a property file to a class, we can add @ConfigurationProperties at a class level

Upvotes: 0

Vinay Prajapati
Vinay Prajapati

Reputation: 7504

@ConfigurationProperties is used when you want to map a set of properties from properties file with a Class properties. Using @ConfigurationProperties make your code more readable, categorized / modular and cleaner. How?

  1. You have application properties mapped to a POJO bean & ensures re-usability.

  2. You are using the spring bean with abstraction (properties are automatically injected).

Now if you use Environment bean, you will always have to call getProperty and then specify the string name of the property, so lots of boilerplate code. Also, if you have to refactor some property and rename it you have to do it in all places it's being used in.

Hence, my recommendation is when you have to group properties and re-use in multiple places go for @ConfigurationProperties. If you have to use a property or two and only in one place throughout application then Environment can be used.

Upvotes: 3

Related Questions