Spring Boot Configuration: Your App’s Control Room 🎛️
Imagine your app is a spaceship. Before launch, you need to set things like speed, destination, and fuel type. You don’t want to rebuild the spaceship every time you change the destination, right?
That’s what Spring Boot Configuration does — it lets you control your app without changing the code.
The Big Picture
Think of configuration like the settings on your phone:
- You can change your ringtone without buying a new phone
- You can switch Wi-Fi networks without reinstalling apps
- You can turn on Dark Mode whenever you want
Spring Boot configuration works the same way for your Java apps!
graph TD A[Your App] --> B[Reads Configuration] B --> C[application.properties] B --> D[application.yml] B --> E[Environment Variables] B --> F[Command Line Args] style A fill:#4ECDC4 style B fill:#FF6B6B
1. Configuration Files: Where Settings Live
The Two Friends: .properties and .yml
Spring Boot looks for settings in special files. The two most common are:
📄 application.properties — Simple key=value pairs
server.port=8080
app.name=MySpaceship
app.max-speed=1000
📄 application.yml — Organized like a tree (YAML format)
server:
port: 8080
app:
name: MySpaceship
max-speed: 1000
Both files do the exact same thing! Pick the one you like better.
Where Do These Files Go?
Put them in your src/main/resources folder:
src/
main/
resources/
application.properties ← Here!
application.yml ← Or here!
Quick Example
Want your app to run on port 9000 instead of 8080?
server.port=9000
That’s it! No code changes needed. Just restart your app.
2. Type-Safe Configuration: No More Typos!
The Problem with Plain Strings
Imagine typing server.prot instead of server.port. Oops! Your app might crash or behave strangely.
The Solution: @ConfigurationProperties
Spring Boot can map your settings to a Java class. This way, your IDE helps you catch typos!
Step 1: Create a class for your settings
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private int maxSpeed;
// Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(int max) {
this.maxSpeed = max;
}
}
Step 2: Enable it in your main class
@SpringBootApplication
@EnableConfigurationProperties(
AppConfig.class
)
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(
MyApp.class, args
);
}
}
Step 3: Use it anywhere!
@Service
public class SpaceshipService {
private final AppConfig config;
public SpaceshipService(AppConfig c) {
this.config = c;
}
public void launch() {
System.out.println(
"Launching " + config.getName()
);
}
}
Why This is Awesome
- ✅ Auto-complete in your IDE
- ✅ Type checking (can’t put text in a number field)
- ✅ Validation (more on this soon!)
- ✅ Organized settings in one place
3. Externalized Configuration: Settings Outside Your Jar
The Problem
Your app works great on your laptop. But what happens when you deploy it to a server? The database URL is different! The API keys are different!
The Solution
Spring Boot can read settings from outside your packaged application:
graph TD A[Spring Boot App] --> B{Where to look?} B --> C[Inside JAR] B --> D[Next to JAR] B --> E[Environment Variables] B --> F[Command Line] style A fill:#4ECDC4 style D fill:#FF6B6B style E fill:#FF6B6B style F fill:#FF6B6B
Ways to Externalize Configuration
1. Environment Variables
export SERVER_PORT=9000
java -jar myapp.jar
Spring Boot converts SERVER_PORT to server.port automatically!
2. Command Line Arguments
java -jar myapp.jar --server.port=9000
3. External File
Put application.properties next to your JAR file:
myapp/
myapp.jar
application.properties ← Spring reads this!
Real-World Example
Your laptop config:
database.url=localhost:5432
Your production server:
export DATABASE_URL=prod-server:5432
Same code, different settings!
4. Configuration Precedence: Who Wins?
The Question
What if you set server.port=8080 in your file, but also pass --server.port=9000 on the command line?
The Answer: There’s a Priority List!
Spring Boot has a clear order of who wins:
graph TD A["1. Command Line Args"] --> B["2. Environment Variables"] B --> C["3. External Config Files"] C --> D["4. Internal Config Files"] D --> E["5. Default Values"] style A fill:#FF6B6B style B fill:#FF9F43 style C fill:#FECA57 style D fill:#48DBFB style E fill:#C8D6E5
Higher = Higher Priority (Wins!)
The Full Priority List (Simplified)
- Command line arguments
--server.port=9000 - Environment variables
SERVER_PORT=9000 - External application.properties (next to JAR)
- Internal application.properties (inside JAR)
- Default values (Spring’s built-in defaults)
Why This Order Makes Sense
Think of it like this:
- Developers set defaults in the code
- DevOps override with external files
- Emergency fixes use command line
Example:
# application.properties (inside JAR)
server.port=8080
# Production override
java -jar app.jar --server.port=80
The app runs on port 80 because command line wins!
5. Profile-Specific Configuration: Different Settings for Different Situations
The Problem
Your app needs:
- Development: Debug mode ON, local database
- Production: Debug OFF, real database
- Testing: Mock services, test database
The Solution: Profiles!
Create separate config files for each situation:
src/main/resources/
application.properties ← Default (always loaded)
application-dev.properties ← Development settings
application-prod.properties ← Production settings
application-test.properties ← Testing settings
How to Use Profiles
application.properties (shared settings)
app.name=MySpaceship
application-dev.properties
server.port=8080
logging.level.root=DEBUG
database.url=localhost:5432
application-prod.properties
server.port=80
logging.level.root=WARN
database.url=prod-db.company.com
Activating a Profile
Option 1: In application.properties
spring.profiles.active=dev
Option 2: Environment variable
export SPRING_PROFILES_ACTIVE=prod
Option 3: Command line
java -jar app.jar --spring.profiles.active=prod
Multiple Profiles
You can activate multiple profiles at once!
java -jar app.jar --spring.profiles.active=prod,secure
This loads:
application.propertiesapplication-prod.propertiesapplication-secure.properties
Profile-Specific Beans
You can even have different code run for different profiles:
@Configuration
@Profile("dev")
public class DevConfig {
@Bean
public DataSource dataSource() {
// Return local database
}
}
@Configuration
@Profile("prod")
public class ProdConfig {
@Bean
public DataSource dataSource() {
// Return production database
}
}
Quick Summary
| Concept | What It Does | Example |
|---|---|---|
| Config Files | Store settings | server.port=8080 |
| Type-Safe | Map to Java class | @ConfigurationProperties |
| Externalized | Settings outside JAR | Environment variables |
| Precedence | Who wins conflicts | Command line > Env > File |
| Profiles | Different settings per environment | application-prod.properties |
You Did It! 🎉
You now understand how Spring Boot Configuration works:
- Configuration files store your settings
- Type-safe configuration prevents typos
- Externalized configuration lets you change settings without rebuilding
- Configuration precedence decides who wins when there are conflicts
- Profile-specific configuration gives you different settings for different environments
Your spaceship is ready to launch with the right settings for any mission! 🚀