java 48 lines · 7 steps

Refreshable feature settings in Spring

A Spring bean caches settings in memory and reloads them on refresh, serving typed reads without hitting the database each time.

Explained by highlit
1@RefreshScope
2@Component
3public class FeatureSettings {
4 
5 private final SettingsRepository repository;
6 
7 private final Map<String, String> cache = new ConcurrentHashMap<>();
8 
9 public FeatureSettings(SettingsRepository repository) {
10 this.repository = repository;
11 }
12 
13 @PostConstruct
14 void load() {
15 cache.clear();
16 repository.findAll().forEach(setting -> cache.put(setting.getKey(), setting.getValue()));
17 }
18 
19 public boolean isEnabled(String feature) {
20 return Boolean.parseBoolean(cache.getOrDefault(feature, "false"));
21 }
22 
23 public int getInt(String key, int defaultValue) {
24 String value = cache.get(key);
25 if (value == null) {
26 return defaultValue;
27 }
28 try {
29 return Integer.parseInt(value.trim());
30 } catch (NumberFormatException ex) {
31 return defaultValue;
32 }
33 }
34 
35 public Duration getDuration(String key, Duration defaultValue) {
36 return Optional.ofNullable(cache.get(key))
37 .map(Duration::parse)
38 .orElse(defaultValue);
39 }
40 
41 public String require(String key) {
42 String value = cache.get(key);
43 if (value == null) {
44 throw new IllegalStateException("Missing required setting: " + key);
45 }
46 return value;
47 }
48}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Loading data once into an in-memory map turns repeated config lookups into cheap reads.
  2. 2@RefreshScope lets a bean rebuild its state at runtime without restarting the application.
  3. 3Typed accessors with defaults keep parsing and null-handling out of every caller.

Related explainers

typescript
import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';
import localeFrExtra from '@angular/common/locales/extra/fr';
import localeDe from '@angular/common/locales/de';

Locale-aware bootstrapping in Angular

i18n localization dependency-injection
Intermediate 8 steps
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
java
@Component
@Converter
public class EncryptedStringConverter implements AttributeConverter<String, String> {
 

Transparent column encryption in Spring & JPA

encryption aes-gcm jpa-converter
Advanced 10 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { InjectRepository } from '@nestjs/typeorm';

A cache-aside country lookup in NestJS

cache-aside dependency-injection batch-lookup
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
typescript
import { Injectable, effect, signal, computed } from '@angular/core';
 
interface Preferences {
  theme: 'light' | 'dark';

A signal-based preferences store in Angular

signals state-management persistence
Intermediate 7 steps

Share this explainer

Here's the card — post it anywhere.

Refreshable feature settings in Spring — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code