Module 3: Application Properties & Profiles (The Settings)
📚 Module 3: Application Properties & Profiles
Focus: Moving from “Hardcoded” to “Configurable.”
A professional app needs to run in different places. It runs on Your Laptop, on a Test Server, and in the Cloud. You don’t want to change your code every time you move it. We use Properties and Profiles.
🏗️ Step 1: The application.properties File
This is the “Control Panel” of your app.
🧩 The Analogy: The Oven Settings
Instead of building a separate oven for “Pizza” and “Cake,” you have one oven with a Dial.
- You change the dial to 400°F for Pizza.
- You change the dial to 325°F for Cake.
In Spring Boot: We use application.properties (or application.yml):
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb🏗️ Step 2: Profiles (The “Environment Switch”)
Sometimes you need completely different settings for different environments.
🧩 The Analogy: The Driving Mode
- Sports Mode: Tight suspension, high fuel use (Production Server - High performance).
- Eco Mode: Soft suspension, low fuel use (Your Laptop - Low resources).
In Code:
application-dev.properties(Eco Mode)application-prod.properties(Sports Mode)
You switch between them with one command: spring.profiles.active=prod.
🏗️ Step 3: @Value (The “Bridge”)
How do we get those settings into our Java code? We use the @Value label.
@Component
public class MyService {
@Value("${app.name}")
private String appName; // This variable now contains the setting from the file!
}🥅 Module 3 Review
- Properties: External settings files for your app.
- Profiles: Groups of settings for different environments (Dev, Test, Prod).
- @Value: Reading a single setting into your code.
- Separation: Keeping your secret passwords and server names OUT of your code.