@ConfigurationProperties로 타입 안전한 구성
속성을 타입이 지정된 구성 클래스에 연결해 보세요.
@ConfigurationProperties로 타입 안전한 구성은(는) CoddyKit의 무료 Spring Boot 4 Microservices & REST APIs 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Spring Boot 4 Microservices & REST APIs 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Spring Boot 4 Microservices & REST APIs 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Beyond @Value
Scattering dozens of @Value annotations across a feature is brittle and hard to validate. @ConfigurationProperties binds a whole group of related keys to a single strongly typed object.
You get IDE auto-completion, type safety, validation, and a clear contract for what the feature reads.
Binding a Prefix to a Class
Declare a class with a prefix; Spring maps each key under that prefix to a matching field. Relaxed binding lets max-retries in YAML map to maxRetries in Java.
@ConfigurationProperties(prefix = "app.mail")
public class MailProperties {
private String host;
private int port;
private int maxRetries;
// getters and setters
}Registering the Properties Bean
A @ConfigurationProperties class must become a bean. The cleanest way is @EnableConfigurationProperties on a configuration class, or @ConfigurationPropertiesScan on the main app.
@SpringBootApplication
@ConfigurationPropertiesScan
public class Application { }
// or explicitly:
@Configuration
@EnableConfigurationProperties(MailProperties.class)
class MailConfig { }The Backing YAML
With the prefix app.mail, these YAML keys bind to the fields. Relaxed binding accepts kebab-case keys for camelCase fields.
app:
mail:
host: smtp.example.com
port: 587
max-retries: 3Injecting the Properties
Inject the typed object anywhere like any other bean. The feature code reads structured config instead of loose strings.
@Service
public class MailService {
private final MailProperties props;
public MailService(MailProperties props) {
this.props = props;
}
void send() {
connect(props.getHost(), props.getPort());
}
}Immutable Binding with Records
Spring Boot supports constructor binding, perfect for immutable config. A Java record works directly with @ConfigurationProperties, no setters needed.
@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(
String host,
int port,
int maxRetries) {
}Default Values
With records you can give defaults in a compact constructor; with classic classes you initialize the field. Defaults apply when the key is absent from the Environment.
@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(String host, Integer port, Integer maxRetries) {
public MailProperties {
if (port == null) port = 587;
if (maxRetries == null) maxRetries = 3;
}
}Nested Objects
Configuration trees map to nested types. A field of a custom type binds keys one level deeper, keeping complex config organized.
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private final Security security = new Security();
public Security getSecurity() { return security; }
public static class Security {
private boolean enabled;
private String issuer;
// getters/setters
}
}Lists and Maps
Collection-typed fields bind to YAML sequences and maps automatically, which is far cleaner than parsing comma-separated strings by hand.
@ConfigurationProperties(prefix = "app")
public class AppProperties {
private List<String> allowedOrigins;
private Map<String, Integer> rateLimits;
// getters/setters
}
// app.allowed-origins: [a, b]
// app.rate-limits: { read: 100, write: 20 }Validating Bound Values
Add @Validated to the properties class and JSR-380 constraints to fields. Spring validates at startup and fails fast if config is invalid, instead of erroring deep in production.
@Validated
@ConfigurationProperties(prefix = "app.mail")
public class MailProperties {
@NotBlank private String host;
@Min(1) @Max(65535) private int port;
// getters/setters
}Metadata and the Processor
Add spring-boot-configuration-processor as an annotation processor to generate metadata. Your IDE then offers auto-complete and docs for your custom keys in application.yml.
<!-- pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>Quick Check
Test your understanding of relaxed binding.
Recap
@ConfigurationProperties gives type-safe, grouped configuration.
- Bind a
prefixto a class or record - Register via
@ConfigurationPropertiesScanor@EnableConfigurationProperties - Records enable immutable constructor binding
- Nested types, lists, and maps bind naturally
@Validatedmakes invalid config fail fast at startup
자주 묻는 질문
“@ConfigurationProperties로 타입 안전한 구성” 강의는 무료인가요?
네 — “@ConfigurationProperties로 타입 안전한 구성” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Spring Boot 4 Microservices & REST APIs 강의 전체를 잠금 해제할 수 있습니다. Spring Boot 4 Microservices & REST APIs 강의에는 총 4개의 강의가 포함되어 있습니다.
“@ConfigurationProperties로 타입 안전한 구성”에서 뭘 배우나요?
속성을 타입이 지정된 구성 클래스에 연결해 보세요. 브라우저에서 직접 실행하는 실습 코드로 Spring Boot 4 Microservices & REST APIs을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Spring Boot 4 Microservices & REST APIs을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Spring Boot 4 Microservices & REST APIs은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“@ConfigurationProperties로 타입 안전한 구성” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Spring Boot 4 Microservices & REST APIs 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Spring Boot 4 Microservices & REST APIs 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- application.properties와 YAML
- 환경별 프로필
- @ConfigurationProperties로 타입 안전한 구성
- 외부 구성과 구성 재정의