Explain Codes LogoExplain Codes Logo

What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?

java
spring-boot
configuration
best-practices
Nikita BarsukovbyNikita Barsukov·Jan 2, 2025
TLDR

The key difference between bootstrap.yml and application.yml lies in the sequence they are accessed during the application startup of a Spring Boot project.

  • bootstrap.yml: Loaded early during the application startup, essential for properties that need to be known before the rest of the context is up.
  • application.yml: Houses the standard configuration loaded when the application context is started, occurring after the bootstrap phase.
# bootstrap.yml contains properties vital in the initial construction phase spring: application: name: my-service cloud: config: uri: http://config-server/
# application.yml introduces more specific configurations during the move-in phase server: port: 8080 spring: datasource: url: jdbc:mysql://localhost/test

Key configurations set within bootstrap.yml will override those in application.yml, making it a valuable tool when you need to fine-tune certain configurations early in application bootstrapping.

Which file for what: Practical applications

Spring Cloud & Microservices

Within Spring Cloud, bootstrap.yml is non-negotiable. Vital configurations like spring.application.name—the name tag for your microservice, and spring.cloud.config.server.git.uri—the repository of your app configurations, are typically set here.

Encryption: No peeking allowed!

If you're dealing with sensitive data that requires encryption or decryption, bootstrap.yml is your partner in crime. Here, security-related configurations are run before the curtains are raised, ensuring your secrets are safe.

Service Discovery: Lost and Found

Planning to use service discovery like Eureka? Details about the Eureka server would live in bootstrap.yml so your app can dutifully register itself first thing during its startup.

Precedence & Overriding: Who's got the last word?

Precedence rules favor bootstrap.yml. This allows you to set global properties in bootstrap.yml and, should the need arise, override them in specific application.yml files.

Logging: Who knew app startup could be so entertaining?

Here's a pro tip: If you enjoy reading system logs for fun (or debugging 😎), placing your logging configuration in bootstrap.yml gives you a front-row seat to the action, from the initial startup.

Making the most of both files

Custom configurations for different app profiles

While both files support profile-specific configs, you can use bootstrap.yml to activate specific default profiles that affect all profiles, even those housed in individual application-{profile}.yml files.

Ordering of configurations matter

When ordering take out, usually the last call counts. It's the same for configurations! Knowing the order of precedence helps you understand how to layer your bootstrap.yml and application.yml files, plus other profile-specific configuration files.

Sleight of hand with environment variables

Both files can be overridden by environment variables at run time or system properties passed as arguments during launch. Handy for environment-specific deployments (think dev, test, prod), this is your secret weapon for tweaking configurations on-the-go.

What pitfalls?

Nobody's perfect - mistakes happen. However, they don't have to if you understand the ins and outs of bootstrap.yml and application.yml. Bypass unintended consequences, like configuration conflicts and unexpected overriding, by knowing when and how to use these files.