AI Hints
Home›Spring Boot Tutorial›application.properties, @Value and @ConfigurationProperties
Spring Boot · Lesson 39 of 120

@ConfigurationProperties

A group of settings.

⏱ 2 min read▶ Video at 51:37🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

When you have many related settings, at Value gets messy. Instead, we can load a whole group of settings into one object.

  • Many settings
  • @Value gets messy
  • One object instead

A settings record

At Configuration Properties shop, loads every key that starts with shop dot. Each record component matches one key. Lists, and durations like three days, are converted for us. And a small config class turns it on, with at Enable Configuration Properties.

ShopProperties.javajava
@ConfigurationProperties("shop")
public record ShopProperties(String name, int discount, List<String> categories,
                             Duration deliveryTime) {}

@Configuration
@EnableConfigurationProperties(ShopProperties.class)
public class ShopConfig {}

The settings

Here are the settings. Names, numbers, and lists, separated by commas. Delivery time, with a dash, matches delivery time in camel case. Spring calls this relaxed binding.

application.propertiesini
shop.name=Corner Books
shop.discount=15
shop.categories=fiction,science,history
shop.delivery-time=3d

Using the group

The controller receives shop properties, like any other bean. The info endpoint returns the whole record, as JSON. And delivery uses the duration, in days. Run it. All four settings were loaded, and converted. Three d became a duration of seventy two hours, which is three days.

ShopController.javajava
@RestController
public class ShopController {

    private final ShopProperties shop;

    public ShopController(ShopProperties shop) {
        this.shop = shop;
    }

    @GetMapping("/info")
    public ShopProperties info() {
        return shop;
    }

    @GetMapping("/delivery")
    public String delivery() {
        return "Delivery takes " + shop.deliveryTime().toDays() + " days";
    }
}
Output
$ curl localhost:8080/info
{
  "name":"Corner Books",
  "discount":15,
  "categories":["fiction","science","history"],
  "deliveryTime":"PT72H"
}
$ curl localhost:8080/delivery
Delivery takes 3 days
Also uses ShopProperties.java
@ConfigurationProperties("shop")
public record ShopProperties(String name, int discount, List<String> categories,
                             Duration deliveryTime) {}
Also uses ShopConfig.java
@Configuration
@EnableConfigurationProperties(ShopProperties.class)
public class ShopConfig {}
Also uses application.properties
shop.name=Corner Books
shop.discount=15
shop.categories=fiction,science,history
shop.delivery-time=3d
Also uses _curl.sh
curl localhost:8080/info
curl localhost:8080/delivery

Summary

At Configuration Properties loads a group of settings into one typed object, with lists and durations converted for you. Next, YAML.

Key takeaways
  • @ConfigurationProperties
  • A typed record
  • Relaxed binding
Next up: YAML — settings with indentation