DEV Community

Spring Boot Configuration: When to Require, Default, or Leave Empty Environment Variables

Spring Boot supports flexible environment variable interpolation in your application.yml files, but it's important to know when to require a variable, leave it empty, or provide a default value.

Choosing the right approach improves reliability, avoids silent misconfigurations, and makes your application easier to maintain across environments.

Letโ€™s break it down into three types:


๐Ÿ”’ 1. ${VARIABLE} โ€” Required

This format means the variable must be defined.

If it's not present, Spring Boot will fail to start with an error.

โœ… When to use:

Use this for critical configuration values your application canโ€™t run without:

  • Database connection details
  • API credentials
  • Encryption keys
spring:
  datasource:
    url: ${DB_URL}       # Must be set
    username: ${DB_USER} # Must be set
    password: ${DB_PASS} # Must be set
Enter fullscreen mode Exit fullscreen mode

๐ŸŸก 2. ${VARIABLE:} โ€” Optional (empty by default)

This syntax allows the variable to be undefined, in which case it defaults to an empty string.

โœ… When to use:

Use when the value is not always required, and you will handle its presence at runtime.

external:
  api-key: ${EXTERNAL_API_KEY:}  # Will be empty if not defined
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Remember to check if it's empty before using it in your code.


๐ŸŸข 3. ${VARIABLE:default} โ€” Optional with fallback value

This format sets a default value if the variable is not defined.

โœ… When to use:

Use when a sensible default exists and makes development easier. This avoids overconfiguring every environment unnecessarily.

server:
  port: ${SERVER_PORT:8080}  # Uses port 8080 if not set
Enter fullscreen mode Exit fullscreen mode

โœ… Best Practices Summary

Purpose Syntax
Required, critical configuration ${VAR}
Optional, check at runtime ${VAR:}
Optional, default fallback ${VAR:default-value}

๐Ÿ“„ Example: application.yml with Best Practices

spring:
  profiles:
    active: ${SPRING_PROFILES_ACTIVE:dev}  # Default to 'dev'

  datasource:
    url: ${DB_URL}           # โ— Required
    username: ${DB_USERNAME} # โ— Required
    password: ${DB_PASSWORD} # โ— Required
    driver-class-name: ${DB_DRIVER:com.mysql.cj.jdbc.Driver}  # โœ… Default fallback

  jpa:
    hibernate:
      ddl-auto: ${HIBERNATE_DDL:auto}  # โœ… Default

server:
  port: ${SERVER_PORT:8080}  # โœ… Default port
  servlet:
    context-path: ${CONTEXT_PATH:/}

client:
  id: ${CLIENT_ID:localhost}     # โœ… Fallback value for local use
  api-key: ${EXTERNAL_API_KEY:}  # ๐Ÿ”ธ Optional

management:
  endpoint:
    env:
      show-values: WHEN_AUTHORIZED  # Safer than ALWAYS
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: health,info

security:
  jwt:
    secret: ${JWT_SECRET}           # โ— Required
    expiration: ${JWT_EXPIRATION:3600}  # โœ… Default in seconds

app:
  environment: ${APP_ENVIRONMENT:local}
  feature-flag:
    enable-beta: ${ENABLE_BETA_FEATURE:false}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›ก๏ธ Final Advice

Always document which environment variables are mandatory and validate them early when possible. This avoids surprises during deployment and improves your systemโ€™s robustness.


Top comments (0)