Skip to main content
PreparationZone Logo
PrepZone

Properties, YAML, and Profiles

Externalised configuration, environment profiles, and binding typed properties.

Pass checkpoint below

Why this matters

  • Hardcoding database URLs or API keys in Java forces recompilation for every environment change.
  • Profiles activate bean sets and property files per environment — dev uses H2, prod uses PostgreSQL.
  • @ConfigurationProperties binds typed configuration objects, replacing scattered @Value injections.

application.yml hierarchy

L1 · Fresher

Properties in application.yml and profile files (application-dev.yml) externalize config. BookStore URLs, pool sizes, and feature flags belong here — not hard-coded in Java.

Profiles for environments

L2 · Intermediate

Activate with spring.profiles.active=dev. Beans annotated @Profile("prod") only load in production — ideal for mock payment clients vs real ones.

@ConfigurationProperties binding

L3 · Advanced

Bind a prefix like bookstore.catalog to a typed record or class with validation. Safer than dozens of @Value fields and easier to test without the context.

Property source precedence

L4 · Expert

Command-line args, OS env vars, and config server overrides beat local YAML. Knowing order explains "it works locally but not in K8s" incidents.

Classpath scanFind @AutoConfiguration classes
@Conditional checksOnClass, OnBean, OnProperty
Register beansOnly if conditions pass
Boot checks classpath and existing beans before creating defaults.

application.yml structure

Java
spring:
  application:
    name: bookstore-api
  datasource:
    url: jdbc:postgresql://127.0.0.1:5432/bookstore
    username: bookstore
    password: ${DB_PASSWORD:localdev}

server:
  port: 8080

bookstore:
  pricing:
    service-url: https://pricing.internal.example.com
    timeout-ms: 3000
  catalog:
    page-size: 20

Spring binds spring.* to built-in properties and custom bookstore.* keys to your @ConfigurationProperties class.

Typed configuration properties

Java
// learn:fragment
@ConfigurationProperties(prefix = "bookstore.pricing")
public record PricingProperties(
    String serviceUrl,
    int timeoutMs
) {}

@Configuration
@EnableConfigurationProperties(PricingProperties.class)
public class BookStoreConfig { }

Injection into services:

Java
// learn:fragment
@Service
public class PricingService {
    private final RestClient client;

    public PricingService(PricingProperties props) {
        this.client = RestClient.builder()
            .baseUrl(props.serviceUrl())
            .build();
    }
}

Records make immutable config objects with validation support via @Validated and @NotBlank.

Profiles

Activate environment-specific configuration:

Java
# application-dev.yml
spring:
  datasource:
    url: jdbc:h2:mem:bookstore
  h2:
    console:
      enabled: true

logging:
  level:
    com.example.bookstore: DEBUG
Java
# application-prod.yml
spring:
  datasource:
    url: jdbc:postgresql://${DB_HOST}:5432/bookstore

Activate with:

Java
java -jar bookstore-api.jar --spring.profiles.active=prod

Or in application.yml:

Java
spring:
  profiles:
    active: dev

Profile strategies

  • application-{profile}.yml — Property overrides per profile.
  • @Profile("dev") on beans — Register beans only in matching environments.
  • spring.profiles.active — Comma-separated list; later profiles override earlier ones.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 6 sections

  • Profile-specific beans
  • @Value for simple cases
  • Property precedence
  • Relaxed binding
  • Validation on startup
  • Beans initialised from configuration

Test yourself

Answer these before moving on — recall is what makes it stick.