In this article, we will discuss application.yml vs bootstrap.yml in Spring Boot and how application.yml or bootstrap.yml are useful in Application development. The application.yml is auto-configured by the Spring Boot framework to handle the development but for the bootstrap.yml we need to configure it externally in Spring Boot. The application.yml or application.properties file configures the application context and bootstrap.yml file is used to configure the bootstrap context.
Usage of application.yml
- This is the main configuration file used by the Spring Boot application.
- It contains configuration properties specific to the application.
- Properties are defined here as loaded after bootstrap.yml is loaded.
Usage of bootstrap.yml
- It is particularly used for setting external configurations like Environment-specific properties.
- Properties are defined here as loaded before application.yml is loaded.
- It is mostly used in microservices architectures to manage independently from the application code.
Example:
Below we provide example configuration properties in both application.yml and bootstrap.yml in Spring Boot.
- It is very useful to understand the difference.
- And we provide the configuration details only.
- After creating Spring Boot project in resource folder, we need to create application.yml and bootstrap.yml files then define your own configuration properties for the project.
Note: Below we provide example structure of application.yml and bootstrap.yml, based on your project, you need modify it.
application.yml:
spring: application: name: my-service datasource: url: jdbc:mysql://localhost:3306/mydatabase username: myuser password: mypassword logging: level: org.springframework: INFO com.example: DEBUG security: oauth2: client: clientId: my-client-id clientSecret: my-client-secret accessTokenUri: http://auth-server/oauth/token userAuthorizationUri: http://auth-server/oauth/authorize cache: caffeine: spec: maximumSize=100,expireAfterAccess=300s messaging: kafka: bootstrap-servers: kafka:9092 consumer: groupId: my-group myapp: greeting: Hello from MyApp!
bootstrap.yml:
spring: profiles: active: dev cloud: config: uri: http://config-server:8888 profile: dev retry: maxAttempts: 3 initialInterval: 1000 multiplier: 1.5 maxInterval: 5000 feign: client: config: default: connectTimeout: 5000 readTimeout: 5000
Difference between application.yml and bootstrap.yml
Aspect
|
application.yml
|
bootstrap.yml
|
Purpose
|
Main configuration file for application specific settings.
|
Configuration file for setting up the application context before the application is created.
|
Loading Order
|
Loaded after bootstrap.yml properties.
|
Loaded before application.yml properties.
|
Configuration Scope
|
Application-specific
|
Primarily used for configuring external configuration sources or common settings across applications.
|
Typical Use Cases
|
Application-specific settings, default configurations, environment specific settings.
|
Configuring Spring Cloud Config Server URI, setting up external configuration sources, common settings across multiple applications.
|
|