Internal Configuration Resolution Order
File-name precedence
bootstrap.yml(orbootstrap.properties) is processed before anyapplication.*file.application.yml(orapplication.properties) is processed next.
Directory precedence (highest → lowest)
./config/– aconfigfolder in the project root./– the project root itselfclasspath:/config/– the packagedconfigdirectory inside the JARclasspath:/– 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|ymlfiles- 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– developmentapplication-test.yml– testapplication-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
application.yml:spring: profiles: active: dev- 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:
- Explicit scan:
@SpringBootApplication @ComponentScan("com.example.shared.config") public class Application { } - Import individual classes:
@SpringBootApplication @Import({SharedConfig.class, SecurityConfig.class}) public class Application { } - Meta-import via custom annotation:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Import(SharedConfig.class) public @interface EnableSharedBeans { } @SpringBootApplication @EnableSharedBeans public class Application { }