Spring Boot externalizes configuration by reading properties from many sources outside your compiled code, such as application.properties or application.yml, profile-specific files, environment variables, command-line arguments and a config server, and then merging them in a fixed order of precedence. You bind the values into your beans with @Value or, preferably, type-safe @ConfigurationProperties classes, so the same JAR runs unchanged in every environment.
Spring Boot builds an Environment from several property sources. When the same key appears in more than one place, the source with higher precedence wins. A simplified order, from lowest to highest, looks like this:
| Priority | Source |
|---|---|
| Lowest | Defaults set with SpringApplication.setDefaultProperties |
application.properties / application.yml packaged in the JAR | |
Profile-specific files such as application-prod.yml packaged in the JAR | |
application.properties / application.yml outside the JAR | |
| OS environment variables | |
Java system properties (-Dkey=value) | |
Command-line arguments (--key=value) | |
| Highest | @TestPropertySource and test properties (tests only) |
The full list in the reference documentation has a few more entries (for example SPRING_APPLICATION_JSON and @PropertySource), but the rule of thumb is that anything closer to the runtime environment overrides what is packaged inside the artifact.
Place defaults in src/main/resources/application.yml and override per environment with profile-specific files.
Activate a profile with SPRING_PROFILES_ACTIVE=prod or --spring.profiles.active=prod. A separate file named application-prod.yml works the same way. Both .properties and .yml are supported, and if both exist for the same name, .properties wins.
Spring Boot looks for config files in the classpath root, the classpath /config package, the current directory and its config/ subdirectory, with later locations overriding earlier ones.
@Value("${app.mail.host}") works for a single value, but a typed class is easier to validate, document and test. A Java record keeps it immutable.
Register it by scanning or explicitly.
Now inject MailProperties anywhere. Binding is relaxed, so app.mail.host, APP_MAIL_HOST and app.mail.Host all map to the host component. If validation fails, the application refuses to start, which is what you want for a missing production setting.
For containers, environment variables are the usual override mechanism. Convert the property name to upper case and replace dots and dashes with underscores.
Never commit passwords to application.yml. Supply them as environment variables, mounted files or a secret manager. Spring Boot can import files with spring.config.import, which also supports the configtree: prefix to read Kubernetes secrets mounted as one file per key.
spring.config.import loads additional files or external sources at startup.
The optional: prefix prevents startup failure when the source is missing. The configserver: form requires the Spring Cloud Config client dependency. Use a config server when many services need centrally managed, versioned configuration. For a small deployment, environment variables are simpler.
Properties can reference each other, with defaults after a colon.
Prefer kebab-case (max-retries) in files; it binds to a maxRetries field. For lists, use YAML sequences or comma-separated values, and for maps, nested keys. Use Duration and DataSize fields to accept values like 30s or 10MB.
Use @ConfigurationProperties for groups of related settings because it provides type safety, validation and IDE metadata. @Value is fine for a single simple value, and supports SpEL.
Set an environment variable such as SERVER_PORT=9090, or mount an application.yml and point to it with spring.config.additional-location. Environment variables override files packaged in the JAR.
Plain Spring Boot does not reload properties. With Spring Cloud you can use @RefreshScope and the refresh endpoint, or redeploy the container, which is usually simpler.
The bootstrap file was used by older Spring Cloud versions to load config server settings early. Current versions use spring.config.import in the normal application file instead.
application.yml, override per profile, and override per deployment with environment variables or arguments.@ConfigurationProperties records and validate them at startup.configtree: or a secret manager.spring.config.import for optional files and config servers.