application.properties & application.yml
Learn how to configure the server port, datasource, and custom properties in a Spring Boot application, and compare .properties and YAML formats.
Introduction
Once auto-configuration has done the heavy lifting of creating your beans, you still need a way to tune specific values — which port the server listens on, which database to connect to, or your own custom application settings. Spring Boot centralizes all of that in one configuration file: `application.properties` or its YAML equivalent, `application.yml`.
application.properties
The `.properties` format uses simple `key=value` pairs, one per line, and lives in `src/main/resources`.
# Server configurationserver.port=8081
# Datasource configurationspring.datasource.url=jdbc:mysql://localhost:3306/demospring.datasource.username=rootspring.datasource.password=secretspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Custom application propertyapp.welcome-message=Welcome to PrograMinds!application.yml
The same configuration can be expressed in YAML, which uses indentation to represent the same nested structure that the dotted `.properties` keys imply.
server: port: 8081
spring: datasource: url: jdbc:mysql://localhost:3306/demo username: root password: secret driver-class-name: com.mysql.cj.jdbc.Driver
app: welcome-message: Welcome to PrograMinds!Properties vs YAML
| Aspect | application.properties | application.yml |
|---|---|---|
| Syntax | Flat key=value pairs | Indentation-based, hierarchical |
| Readability for deep nesting | Repeats common prefixes on every line | Groups related keys visually |
| Comments | Uses # | Uses # |
| Multiple profiles in one file | Not supported directly | Supported using --- document separators |
| Common usage | Very common, simple, familiar | Preferred for complex or multi-profile configs |
A Spring Boot project should use either application.properties or application.yml, not both at once for the same values — pick one format for consistency across the project.
Custom Properties
Beyond configuring Spring Boot's own auto-configured beans, you can define your own custom properties (like `app.welcome-message` above) and inject them into your own beans using `@Value`.
import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class WelcomeController {
@Value("${app.welcome-message}") private String welcomeMessage;
@GetMapping("/api/welcome") public String welcome() { return welcomeMessage; }}Click Run to see what this code prints.
Common Mistakes
- Keeping both application.properties and application.yml in the same project with overlapping keys, causing confusing precedence issues.
- Mixing tabs and spaces in application.yml — YAML is strictly whitespace-sensitive and will fail to parse.
- Committing real database passwords directly into application.properties in a public repository instead of using environment variables or a secrets manager.
Best Practices
- Pick one configuration format (properties or YAML) per project and stick with it.
- Use environment variables or a secrets manager for sensitive values in production, not hardcoded plaintext.
- Group custom application properties under your own prefix (e.g. app.*) to keep them clearly separate from Spring's own properties.
- Use @ConfigurationProperties instead of many individual @Value fields once you have more than a handful of custom settings.
Frequently Asked Questions
Technically yes, but it is not recommended. If both exist, Spring Boot merges them but properties files take precedence over YAML by default, which can cause confusing, hard-to-trace behavior.
Create a profile-specific file such as application-prod.properties or application-prod.yml, then activate it with spring.profiles.active=prod.
@Value injects a single property into a single field. @ConfigurationProperties binds an entire group of related properties onto a single strongly-typed Java class, which scales much better for larger configuration sets.
Key Takeaways
- application.properties and application.yml both live in src/main/resources and configure your application.
- YAML is hierarchical and often more readable for deeply nested configuration.
- Custom properties can be injected into beans using @Value or, at scale, @ConfigurationProperties.
- Sensitive values should never be committed in plaintext to a public configuration file.
Summary
application.properties and application.yml are where you fine-tune what auto-configuration set up for you, from the server port to database credentials to your own custom settings. Next, you will look at Spring Boot DevTools, a small dependency that makes the local development loop dramatically faster.