The 30 second answer
application.properties or application.yml holds settings like the port and database URL. I read a single value with @Value, but for a group of settings I bind them to a typed class with @ConfigurationProperties, which is safer and can be validated. Profiles such as dev and prod load extra files like application-prod.yml on top of the defaults. Environment variables and command-line arguments override the files, so the same JAR runs everywhere and secrets stay out of the code.
<!-- level: 1 --> <!-- must_know: true -->
# application.yml
server:
port: 8080
payments:
base-url: https://payments.internal
timeout: 2s@ConfigurationProperties(prefix = "payments")
public record PaymentsProperties(URI baseUrl, Duration timeout) { }- **
@Value("${payments.base-url}")** injects one value. It's fine for one or two settings. - **
@ConfigurationProperties** binds a whole prefix to a type, supports relaxed names (base-url,baseUrl,PAYMENTS_BASEURL), converts types likeDuration, and works with@Validated. Register it with@ConfigurationPropertiesScanor@EnableConfigurationProperties. - Override order. Spring Boot reads many property sources in a documented order. Later sources win, so environment variables and command-line arguments override
application.ymlinside the JAR. That is how one build runs in dev, staging and production. - Profiles.
spring.profiles.active=prodloadsapplication-prod.ymlon top ofapplication.yml. Beans can be limited with@Profile("prod"). - Secrets such as database passwords come from environment variables or a secret store, never from a committed file.
The detailed loading rules are on How does Spring Boot load application.properties internally? and How does Spring Boot load profile-specific configurations?.
Sources (2)
- Spring Boot reference: Externalized configurationdocs.spring.io
- Spring Boot reference: Profilesdocs.spring.io