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
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
java
public class SortedListMerger { public static int[] merge(int[] a, int[] b) { int[] result = new int[a.length + b.length];
Merging two sorted arrays in Java
two-pointers
merging
arrays
Beginner
6 steps
php
<?php namespace App\Support;
Merging query params onto a URL in PHP
url-parsing
query-strings
immutability
Intermediate
8 steps
java
import java.util.ArrayDeque; import java.util.Deque; public final class RollingAverage {
A rolling average over a sliding window
sliding-window
running-sum
deque
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.