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 and Profiles is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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;
}

@ConfigurationProperties for Type-Safe Binding

Bind a group of related properties to a Java record or class annotated with @ConfigurationProperties. Validation annotations are supported.

@ConfigurationProperties(prefix = "app.mail")
public record MailProperties(
    @NotBlank String host,
    @Min(1) @Max(65535) int port,
    boolean ssl
) {}
// Bind in Spring Boot 3: @EnableConfigurationProperties(MailProperties.class)

Profiles: Defining Environments

Profiles let you have environment-specific configurations. Define beans or properties active only when a certain profile is active.

# application-dev.properties (active when profile=dev):
spring.datasource.url=jdbc:h2:mem:testdb
logging.level.com.example=DEBUG

# application-prod.properties (active when profile=prod):
spring.datasource.url=jdbc:postgresql://prod-db/app
logging.level.com.example=WARN

Activating Profiles

Activate a profile via application.properties, environment variable, system property, or Spring Boot's SPRING_PROFILES_ACTIVE.

# application.properties:
spring.profiles.active=dev
# Or environment variable:
export SPRING_PROFILES_ACTIVE=prod
# Or JVM argument:
java -Dspring.profiles.active=prod -jar app.jar

@Profile on Beans

Annotate a @Bean or @Component with @Profile("prod") to register it only when the matching profile is active.

@Configuration
public class DataSourceConfig {
    @Bean @Profile("dev")
    public DataSource h2DataSource() { return new EmbeddedDatabaseBuilder().build(); }
    @Bean @Profile("prod")
    public DataSource postgresDataSource() { return DataSourceBuilder.create().url(prodUrl).build(); }
}

Profile Groups (Spring Boot 2.4+)

Group multiple profiles under one name. Activating the group activates all listed profiles simultaneously.

# application.properties:
spring.profiles.group.production=prod,metrics,cloud

Externalized Configuration with Environment Variables

Spring Boot automatically maps environment variables to properties. DB_HOST maps to db.host (relaxed binding). Ideal for Docker and Kubernetes deployments.

# application.properties:
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASS}

Property Overriding Priority

Properties are applied in priority order: command-line args > environment variables > profile-specific files > application.properties > default values. Higher-priority sources override lower ones.

Encrypting Sensitive Properties with Jasypt

Sensitive values like passwords can be encrypted with Jasypt Spring Boot. Store the encrypted value in properties; Jasypt decrypts at startup using a master key passed as a JVM arg.

# application.properties:
spring.datasource.password=ENC(k9r/2dfhXPQz+2jR8yzUYw==)
# JVM arg:
java -Djasypt.encryptor.password=masterSecret -jar app.jar

Multi-Document YAML Files

YAML files support multiple documents separated by ---. Use spring.config.activate.on-profile within each document for inline profile-specific config.

server:
  port: 8080
---
spring:
  config:
    activate:
      on-profile: prod
server:
  port: 443

Quick Check

Which annotation binds a group of related properties to a Java class with type safety?

Recap

Use application.properties / application.yml for config. @Value for single values; @ConfigurationProperties for groups. Profiles separate environments. Environment variables override properties — essential for containers.

Frequently asked questions

Is the “Application Properties and Profiles” lesson free?

Yes — the full text of “Application Properties and Profiles” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Application Properties and Profiles”?

Externalize configuration with application.properties/yml, define profiles, and inject values with @Value and @ConfigurationProperties. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Application Properties and Profiles” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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