0Pricing
Java Academy · Lesson

Application Properties and Profiles

Externalize configuration with application.properties/yml, define profiles, and inject values with @Value and @ConfigurationProperties.

application.properties vs application.yml

Spring Boot reads configuration from src/main/resources/application.properties or application.yml. YAML supports hierarchical config without key repetition; both are equivalent in features.

# application.properties:
server.port=8080
spring.datasource.url=jdbc:postgresql://localhost/mydb
spring.jpa.hibernate.ddl-auto=validate

# Equivalent application.yml:
server:
  port: 8080
spring:
  datasource:
    url: jdbc:postgresql://localhost/mydb
  jpa:
    hibernate:
      ddl-auto: validate

Injecting Values with @Value

Use @Value("${property.key}") to inject a single property. Add a default with ${key:default}. SpEL expressions are supported.

@Service
public class PricingService {
    @Value("${pricing.vat-rate:0.20}")
    private double vatRate;
    @Value("${app.name}")
    private String appName;
}

All lessons in this course

  1. Auto-Configuration and Spring Boot Starters
  2. Application Properties and Profiles
  3. Bean Wiring: @Component, @Service, @Repository
  4. Constructor Injection and Circular Dependencies
← Back to Java Academy