java
48 lines · 6 steps
Type-safe config with Spring records
A Java record binds and validates payments.gateway.* properties at startup using Spring Boot's @ConfigurationProperties.
Explained by
highlit
1package com.example.payments.config;
2
3import jakarta.validation.constraints.Min;
4import jakarta.validation.constraints.NotBlank;
5import jakarta.validation.constraints.NotNull;
6import java.time.Duration;
7import java.util.List;
8import org.springframework.boot.context.properties.ConfigurationProperties;
9import org.springframework.boot.context.properties.bind.DefaultValue;
10import org.springframework.validation.annotation.Validated;
11
12@Validated
13@ConfigurationProperties(prefix = "payments.gateway")
14public record PaymentGatewayProperties(
15
16 @NotBlank
17 String baseUrl,
18
19 @NotBlank
20 String apiKey,
21
22 @NotNull
23 @DefaultValue("5s")
24 Duration connectTimeout,
25
26 @NotNull
27 @DefaultValue("30s")
28 Duration readTimeout,
29
30 @Min(0)
31 @DefaultValue("3")
32 int maxRetries,
33
34 @DefaultValue("true")
35 boolean tlsVerification,
36
37 @NotNull
38 Retry retry,
39
40 @DefaultValue
41 List<String> supportedCurrencies
42) {
43
44 public record Retry(
45 @Min(1) @DefaultValue("2") int attempts,
46 @NotNull @DefaultValue("500ms") Duration backoff
47 ) {}
48}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Java records make immutable configuration objects where each component is a named, typed property.
- 2Combining @Validated with Jakarta constraints fails fast at startup when config is missing or invalid.
- 3@DefaultValue lets Spring supply sensible fallbacks so only secrets and overrides need to be set.
Related explainers
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/type-safe-config-with-spring-records-explained-java-77aa/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.