java 44 lines · 7 steps

Loading typed config into a Java record

A record models server settings and a set of static factory methods parse them safely from a properties file.

Explained by highlit
1public record ServerConfig(
2 String host,
3 int port,
4 Duration timeout,
5 boolean tlsEnabled,
6 List<String> allowedOrigins) {
7 
8 public static ServerConfig load(Path file) {
9 Properties props = new Properties();
10 try (InputStream in = Files.newInputStream(file)) {
11 props.load(in);
12 } catch (IOException e) {
13 throw new UncheckedIOException("Failed to read config: " + file, e);
14 }
15 return fromProperties(props);
16 }
17 
18 public static ServerConfig fromProperties(Properties props) {
19 return new ServerConfig(
20 require(props, "server.host"),
21 Integer.parseInt(require(props, "server.port")),
22 Duration.ofSeconds(Long.parseLong(props.getProperty("server.timeoutSeconds", "30"))),
23 Boolean.parseBoolean(props.getProperty("server.tls.enabled", "false")),
24 parseList(props.getProperty("server.allowedOrigins", "")));
25 }
26 
27 private static String require(Properties props, String key) {
28 String value = props.getProperty(key);
29 if (value == null || value.isBlank()) {
30 throw new IllegalStateException("Missing required config key: " + key);
31 }
32 return value.trim();
33 }
34 
35 private static List<String> parseList(String raw) {
36 if (raw.isBlank()) {
37 return List.of();
38 }
39 return Arrays.stream(raw.split(","))
40 .map(String::trim)
41 .filter(s -> !s.isEmpty())
42 .toList();
43 }
44}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Records give you an immutable, self-documenting data carrier with almost no boilerplate.
  2. 2Static factory methods keep parsing and validation logic beside the type they build.
  3. 3Fail loudly on missing required values but fall back to sensible defaults for optional ones.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Loading typed config into a Java record — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code