Spring application.yml

Spring Boot can keep profile-specific configuration and receive secrets separately. If a committed application.yml or application-prod.yml contains a live database password, token or signing key, revoke or disable the exposed credential first and issue a replacement.

Examples

Pattern A: Inject values through ${ENV_VAR}

Leave ${ENV_VAR} references in the configuration file and supply the values from the deployment environment.

When to choose this pattern

Use this option when the deployment supplies environment variables and only a few secrets are needed. Existing profile-specific configuration can stay in place.

Before

yaml
spring:
  datasource:
    url: jdbc:postgresql://prod-db.internal:5432/app
    username: app_user
    password: super-secret-password

jwt:
  secret: hardcoded-jwt-secret

After

yaml
spring:
  datasource:
    url: ${DB_URL}
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}

jwt:
  secret: ${JWT_SECRET}
Spring code example
java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.constraints.NotBlank;

@Validated
@ConfigurationProperties(prefix = "jwt")
public class JwtProperties {

    @NotBlank
    private String secret;

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }
}

This is an excerpt for a Spring Boot environment using Jakarta Validation. Add the validation dependency and register the class through @ConfigurationPropertiesScan or @EnableConfigurationProperties. Stop startup if required values are missing or empty.

Pattern B: Mount an application.yml file

If the application must read a real application.yml or application-prod.yml, mount the file at deployment in a location such as /config/application.yml or /etc/app/application.yml.

When to choose this pattern

Use this option to retain the profile-specific file layout and external configuration path. Check whether Kubernetes or your VM environment can supply the file through a Secret volume, Vault Agent or protected file delivery.

File to keep in the repository

yaml
spring:
  datasource:
    url: jdbc:postgresql://prod-db.internal:5432/app
    username: app_user
    password: "<runtime-provided>"

jwt:
  secret: "<runtime-provided>"

Example runtime paths

text
/config/application.yml
/etc/app/application.yml

Spring configuration example

Use a standard Spring Boot external configuration location or specify the runtime file with spring.config.import as shown below.

yaml
spring:
  config:
    import: file:/etc/app/application.yml

This file is required, so the import does not use optional:. Do not commit production application.yml or application-prod.yml files to Git.

Developer responsibilities

Remove production values from tracked configuration and have the application read environment variables or an external file. Use @ConfigurationProperties or the existing loader to validate required values and stop startup if one is missing or empty.

Infrastructure and platform responsibilities

Supply the variables or file at deployment. Keep live values out of Helm values files, Docker images and ConfigMaps. Define how to restart or reload the application so replacements take effect after rotation.

Verification

  • Verify that the production application-prod.yml is absent from Git and that the deployment supplies values through ${ENV_VAR} references or the mounted file.
  • Test that startup stops when a required value is missing. An empty default must not mask the omission.
  • Check /actuator/env, error responses, stack traces and debug logs for exposed values.

Related documentation