पाठ 6 / 25
Typed Configuration Properties
Bind settings to records.
@ConfigurationProperties
Settings live in application.properties or application.yaml and can be overridden by environment variables, command-line arguments and profile-specific files. @ConfigurationProperties(prefix = "shop") binds related settings to a typed class or record (shop.max-page-size maps to maxPageSize), which is safer than scattering @Value strings. Register it with @EnableConfigurationProperties or scanning, and validate it with Bean Validation annotations if needed.
application.properties and the bound record
This file is from a demo "shop" service generated by start.spring.io for Spring Boot 4.1.1 and Java 21 (Temurin 21.0.12), built with Maven 3.9.16; the project compiled and its tests passed. Both custom keys bind to ShopProperties; the datasource uses an in-memory H2 database and open-in-view is disabled.
spring.application.name=shop
spring.datasource.url=jdbc:h2:mem:shop
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.open-in-view=false
management.endpoints.web.exposure.include=health,info
management.endpoint.health.show-details=always
shop.max-page-size=50
shop.currency=INR
# ShopProperties.java
package com.example.shop;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "shop")
public record ShopProperties(int maxPageSize, String currency) { }Override with environment variables
SHOP_CURRENCY=USD overrides shop.currency at runtime without rebuilding, thanks to relaxed binding.
त्वरित जाँच: What does @ConfigurationProperties(prefix = "shop") do?
- Defines a REST endpoint
- Creates a database table
- Disables configuration
- Binds shop.* settings to a typed class or record
Answer
Binds shop.* settings to a typed class or record — Type-safe configuration.