Spring Boot Configuration Loading Order and YAML Essentials

Internal Configuration Resolution Order

File-name precedence

  1. bootstrap.yml (or bootstrap.properties) is processed before any application.* file.
  2. application.yml (or application.properties) is processed next.

Directory precedence (highest → lowest)

  1. ./config/ – a config folder in the project root
  2. ./ – the project root itself
  3. classpath:/config/ – the packaged config directory inside the JAR
  4. classpath:/ – the root of the classpath

Externalized Configuration

Spring Boot continues to look for configuration in the following places after the internal ones:

  • OS environment variables
  • Java system properties (-Dkey=value)
  • Command-line arguments (--key=valuen- JNDI attributes (java:comp/env)
  • application-{profile}.properties|yml files
  • Default property sources

YAML Syntax Cheat-Sheet

# Case-sensitive
key: value          # mandatory space after colon
nested:             # indentation shows hierarchy
  child: 42
  # no tabs, only spaces (amount doesn’t matter as long as siblings align)

# Comment until end of line

# Map / Object
person:
  name: Alice
  age: 30
# inline form
person: {name: Alice, age: 30}

# Sequence / Array
cities:
 - Beijing
 - Shanghai
# inline form
cities: [Beijing, Shanghai]

# Scalars
plain: Hello World
single: 'Line\nbreak ignored'
double: "Line\nbreak preserved"

# Reference
baseDir: /opt/app
downloadDir: ${baseDir}/downloads

Injecting Properties

@Component
public class AppProps {

    @Value("${app.name}")
    private String applicationName;

    @Value("${person.age}")
    private int userAge;

    @Value("${cities[0]}")
    private String primaryCity;

    @Value("${single}")
    private String literal;
}

Multi-Environment Profiles

Separate files

  • application-dev.yml – development
  • application-test.yml – test
  • application-prod.yml – production

Single file with documents

spring:
 profiles:
   active: dev
---
spring:
 config:
   activate:
     on-profile: dev
server:
 port: 8080
---
spring:
 config:
   activate:
     on-profile: prod
server:
 port: 80

Activating a profile

  1. application.yml:
    spring:
      profiles:
        active: dev
    
  2. JVM arguement:-Dspring.profiles.active=dev 3.nCommand line:
    java -jar app.jar --spring.profiles.active=prod
    

File Format Preference

In the same directory: application.properties > application.yml > application.yaml

Excluding Embedded Tomcat

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>

Sharing Beens Across Modules

@SpringBootApplication only scans the package in which the main class resides and its sub-pacakges. To use beans defined elsewhere:

  1. Explicit scan:
    @SpringBootApplication
    @ComponentScan("com.example.shared.config")
    public class Application { }
    
  2. Import individual classes:
    @SpringBootApplication
    @Import({SharedConfig.class, SecurityConfig.class})
    public class Application { }
    
  3. Meta-import via custom annotation:
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Import(SharedConfig.class)
    public @interface EnableSharedBeans { }
    
    @SpringBootApplication
    @EnableSharedBeans
    public class Application { }
    

Tags: spring-boot configuration YAML profiles dependency-management

Posted on Thu, 25 Jun 2026 17:51:30 +0000 by 4evernomad