Configuration management is one of the most important topics for maintainable applications.

In real projects, values can come from many places:

  • application.yml
  • environment variables
  • Docker Config
  • Kubernetes ConfigMaps
  • Kubernetes Secrets
  • Spring profiles (dev, prod, …)
  • command line arguments

In this post I will show:

  • a datasource configuration
  • a REST client configuration
  • secret handling
  • Kubernetes deployment
  • local development setup

1. Basic application.yml

Spring Boot commonly uses YAML for configuration.

server:
port: 8080
app:
rest-client:
url: http://localhost:9090
timeout: 5000
spring:
datasource:
url: jdbc:postgresql://localhost:5432/demo
username: demo
password: demo

2. Inject Values with @Value

The simplest way is using @Value.

@Service
public class MyService {
@Value("${app.rest-client.url}")
private String url;
@Value("${app.rest-client.timeout}")
private int timeout;
public void printConfig() {
System.out.println(url);
System.out.println(timeout);
}
}

3. Using @ConfigurationProperties

This is the preferred approach in larger applications.

YAML

app:
rest-client:
url: http://localhost:9090
timeout: 5000

Properties Class

@ConfigurationProperties(prefix = "app.rest-client")
public class XClientProperties {
// Default value if not configured
private String url = "http://default-url";
private int timeout = 3000;
// getters and setters
}

Usage

@Service
@RequiredArgsConstructor
public class ClientService {
private final XClientProperties properties;
public void call() {
System.out.println("Rest Client Url: " + properties.getUrl());
}
}

Advantages:

  • strongly typed
  • cleaner code
  • default values possible
  • easier testing

This is usually the best approach for enterprise projects.


4. Default Values Directly in YAML

Spring also supports defaults directly in YAML placeholders.

app:
rest-client:
url: ${CLIENT_URL:http://localhost:9090}

Meaning:

  • use CLIENT_URL if defined
  • otherwise use http://localhost:9090

5. Environment Variables

Spring Boot automatically maps environment variables.

Example:

export APP_REST_CLIENT_URL=http://prod-api

Spring converts:

APP_REST_CLIENT_URL

to:

app.rest-client.url

This is commonly used in:

  • Docker
  • Kubernetes
  • OpenShift
  • CI/CD pipelines

6. Spring Profiles (dev and prod)

Profiles are essential.

application.yml

spring:
profiles:
active: dev

application-dev.yml

app:
rest-client:
url: http://localhost:9090
spring:
datasource:
url: jdbc:postgresql://localhost:5432/devdb

application-prod.yml

app:
rest-client:
url: https://prod-api.company.com
spring:
datasource:
url: jdbc:postgresql://prod-db:5432/proddb

Run with profile:

java -jar app.jar --spring.profiles.active=prod

or:

export SPRING_PROFILES_ACTIVE=prod

7. Docker Environment Variables

Dockerfile

FROM eclipse-temurin:21
COPY target/demo.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Run Container

docker run \
-e SPRING_PROFILES_ACTIVE=prod \
-e APP_REST_CLIENT_URL=http://real-api \
-e SPRING_DATASOURCE_PASSWORD=secret \
demo-app

8. Docker Compose

Very useful for local development.

docker-compose.yml

version: '3.9'
services:
postgres:
image: postgres:16
environment:
POSTGRES_DB: demo
POSTGRES_USER: demo
POSTGRES_PASSWORD: demo
app:
build: .
ports:
- "8080:8080"
environment:
SPRING_PROFILES_ACTIVE: dev
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/demo
SPRING_DATASOURCE_USERNAME: demo
SPRING_DATASOURCE_PASSWORD: demo
APP_REST_CLIENT_URL: http://mock-api:9090

9. Kubernetes ConfigMap

ConfigMaps store non-sensitive configuration.

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: demo-config
data:
APP_REST_CLIENT_URL: "http://prod-api"
SPRING_PROFILES_ACTIVE: "prod"

10. Kubernetes Secret

Secrets should store passwords and tokens.

Create Secret

apiVersion: v1
kind: Secret
metadata:
name: demo-secret
type: Opaque
stringData:
SPRING_DATASOURCE_PASSWORD: mySecretPassword

11. Recommended Enterprise Approach

In most enterprise Spring Boot projects, I recommend:

Use

  • @ConfigurationProperties
  • profile-based YAML
  • environment variables
  • Kubernetes ConfigMaps
  • Kubernetes Secrets

Avoid

  • too many scattered @Value
  • hardcoded secrets
  • duplicated configuration

12. Common Configuration Priority

Spring Boot configuration priority is roughly:

  1. command line arguments
  2. environment variables
  3. application-prod.yml
  4. application.yml
  5. default values in Java classes

This is important during debugging.

Hinterlasse einen Kommentar

I’m Iman

Mein Name ist Iman Dabbaghi. Ich arbeite als Senior Software Engineer in der Schweiz. Außerdem interessiere ich mich sehr für gewaltfreie Kommunikation, Bachata-Tanz und Musik sowie fürs die Persönlichkeitsentwicklung.

Ich habe einen Masterabschluss in Informatik von der Universität Freiburg in Deutschland, bin Spring/Java Certified Professional (OCP), Certified Professional for Software Architecture (CPSA-F) und ein lebenslanger Lernender 🎓.

EN:

My name is Iman Dabbaghi. I work as a Senior Software Engineer in Switzerland. I am also very interessted in nonviolent communication, Bachata dance and music and also for personal development.

I hold a masters degree in computer science from the university of Freiburg in Germany, am a Spring / Java Certified Professional (OCP), Certified Software Architecture (CPSA-F) and Life Long Learner🎓

Let’s connect