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: 8080app: rest-client: url: http://localhost:9090 timeout: 5000spring: datasource: url: jdbc:postgresql://localhost:5432/demo username: demo password: demo
2. Inject Values with @Value
The simplest way is using @Value.
@Servicepublic 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@RequiredArgsConstructorpublic 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_URLif 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:9090spring: datasource: url: jdbc:postgresql://localhost:5432/devdb
application-prod.yml
app: rest-client: url: https://prod-api.company.comspring: 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:21COPY target/demo.jar app.jarENTRYPOINT ["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: v1kind: ConfigMapmetadata: name: demo-configdata: APP_REST_CLIENT_URL: "http://prod-api" SPRING_PROFILES_ACTIVE: "prod"
10. Kubernetes Secret
Secrets should store passwords and tokens.
Create Secret
apiVersion: v1kind: Secretmetadata: name: demo-secrettype: OpaquestringData: 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:
- command line arguments
- environment variables
- application-prod.yml
- application.yml
- default values in Java classes
This is important during debugging.

Hinterlasse einen Kommentar