HuntExams Academy logo
JavaBeginner#spring

What is the difference between application.properties and application.yml in Spring Boot?

Answer

They are two file formats for the same Spring Boot configuration mechanism. Both are read into the Environment, support the same property keys, the same relaxed binding into @ConfigurationProperties, the same placeholder syntax and the same profile mechanism. The choice is about readability, not capability. application.properties uses flat key=value lines: server.port=8081 spring.datasource.url=jdbc:mysql://localhost:3306/app application.yml uses an indented hierarchy, so a shared prefix is written once: server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3306/app Practical differences: - YAML is far more compact for deep nesting, supports lists naturally with "- item", and allows several profiles in one file separated by ---. Properties needs indexed keys such as app.hosts[0]=... - YAML is whitespace-sensitive: tabs are illegal and a wrong indent silently changes meaning. Properties is dumb but very hard to break. - YAML infers types, so quote carefully. Values like on, off, yes and no can become booleans, and version strings such as 1.10 can be read as numbers. Quote passwords containing special characters. - If both files exist in the same location, .properties wins for duplicate keys because it is loaded later. - @PropertySource supports .properties but not YAML directly. - Kubernetes ConfigMaps and Spring Cloud Config commonly use YAML, so it is the more common choice in cloud deployments. Precedence is unchanged either way: command-line arguments and environment variables beat file values, profile-specific files such as application-prod.yml beat the base file, and an external /config folder beats the packaged classpath copy.

Example

# application.properties
server.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/app
spring.datasource.username=root
spring.jpa.hibernate.ddl-auto=validate
app.allowed-hosts[0]=api.example.com
app.allowed-hosts[1]=admin.example.com

# application.yml -- the same configuration
server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/app
    username: root
  jpa:
    hibernate:
      ddl-auto: validate
app:
  allowed-hosts:
    - api.example.com
    - admin.example.com

# Two profiles in ONE yml file
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 8081
---
spring:
  config:
    activate:
      on-profile: prod
server:
  port: 80

# Quote values YAML would misread
app:
  feature-flag: "on"     # unquoted -> boolean true
  version: "1.10"        # unquoted -> the number 1.1

Detailed explanation

application.properties and application.yml are two formats for supplying configuration to Spring Boot, not different configuration mechanisms. Both support the same property names, placeholders such as ${DB_URL}, profile-based configuration, and relaxed binding through @ConfigurationProperties. For example, server.port=8081 in properties is equivalent to server:\n port: 8081 in YAML. Choose based on readability, configuration structure, and team conventions rather than assuming either format offers more Spring Boot features. Spring Boot also recognises the .yaml extension.

A properties file normally expresses configuration as flat key=value entries, repeating prefixes such as spring.datasource for related settings. YAML expresses the same structure through indentation, making nested configuration easier to scan. Lists use indexed keys such as app.allowed-hosts[0]=api.example.com in properties, while YAML allows a sequence of '- item' entries beneath allowed-hosts. Under the hood, Spring Boot loads these files into property sources exposed through its Environment. YAML mappings and sequences are represented as property paths and indexed entries; the Binder then converts values into the fields and types declared by @ConfigurationProperties classes.

YAML requires greater care with syntax. Use spaces for indentation: tabs cannot be used for indentation, and incorrect nesting can produce valid YAML with unintended property names. YAML scalar parsing may also interpret values as numbers or booleans; quoting a version such as "1.10" preserves its string representation. Treatment of words such as on and off depends on the YAML parser and schema, so quote them when they must remain strings. Properties avoids indentation-related mistakes but still has escaping and continuation rules.

Configuration precedence is determined by property-source ordering, not readability. If both formats exist in the same location, properties takes precedence for overlapping keys. Command-line arguments and environment variables normally override application-file values, while profile-specific files override their corresponding base files. YAML supports multiple documents separated by ---, but modern Spring Boot also supports multi-document properties using a #--- separator. Both can activate documents with spring.config.activate.on-profile. Finally, @PropertySource does not load YAML directly without custom support. Interviewers generally expect equivalent functionality, syntax differences, precedence, and practical pitfalls—not an unsupported claim that YAML is inherently better for cloud deployments.

Key points to remember

  • Both formats populate Spring Boot’s Environment and support the same configuration keys and binding features.
  • Properties uses flat entries; YAML uses indentation, nested mappings, and natural list syntax.
  • YAML needs careful indentation and quoting; properties has its own escaping rules.
  • For overlapping keys in the same location, application.properties takes precedence over application.yml.
  • Both support profiles and, in modern Spring Boot, multiple documents; @PropertySource does not support YAML directly.

Common mistakes candidates make

  • Claiming that only YAML supports lists, profiles, or multi-document configuration.
  • Using tabs for YAML indentation or leaving string-like numbers unquoted.
  • Assuming YAML always overrides properties, or that file values override environment variables.

Likely follow-up questions

What happens if both files define server.port?

When both files are in the same location, the value in application.properties takes precedence. A higher-priority source, such as a command-line argument, can still override that value.

How do you configure different environments?

Use files such as application-dev.properties or application-prod.yml and activate the required profile, for example with --spring.profiles.active=prod. Alternatively, use separate documents activated through spring.config.activate.on-profile.

Does @ConfigurationProperties work differently with YAML?

No; it binds from the Environment rather than depending on the original file format. Equivalent properties entries and YAML structures bind to the same Java fields, including nested objects and lists.

Interview tip: Start with “same configuration mechanism, different syntax,” show one equivalent property, and then explain readability, precedence, and YAML pitfalls.

Related Interview Questions

1
JavaBeginner#spring

What is the purpose of Spring Boot starters?

Open
2
JavaIntermediate#spring

What is the difference between @RequestParam, @PathVariable, and @RequestBody?

Open
3
JavaIntermediate#spring

What are Spring bean scopes?

Open