Spring Boot Profiles Notes
Spring Boot Profiles Notes
Spring Boot Profiles allow developers to manage different configurations for different
environments like development, testing, staging, and production using the same code base.
1. Real-Life Example
Imagine you are building a shopping app. You want:
Instead of changing code manually each time, you use profiles to manage environment-
specific settings.
- application.properties (default)
- application-dev.properties (for development)
- application-prod.properties (for production)
3. Configuration Example
application.properties
spring.profiles.active=dev
application-dev.properties
logging.level.org.springframework=TRACE
application-prod.properties
logging.level.org.springframework=INFO
4. How It Works
- Spring Boot reads application.properties and then application-dev or application-prod
based on active profile.
5. Logging Levels
Logging Level Description
6. Conclusion
Spring Boot's profile system allows you to manage environment-specific configuration
easily by creating profile-specific property files and activating them using
spring.profiles.active.
✅ 1. Real-Life Problem:
Development (dev)
Quality Assurance (QA)
Production (prod)
Each of these environments needs different settings. For example, a currency service
might need:
Different URLs
Different usernames
Different API keys
✅ 2. Spring Boot Solution:
▶ @ConfigurationProperties
Helps you bind multiple related config values into one Java class.
Recommended when you have a group of related settings.
▶ Profiles
✅ 3. Step-by-Step Guide:
@Autowired
private CurrencyServiceConfiguration configuration;
@GetMapping("/currency-configuration")
public CurrencyServiceConfiguration getConfiguration() {
return configuration;
}
}
Visit: http://localhost:8080/currency-configuration
You will get JSON with the default values.
✅ 5. Using Profiles
▶ Create application-dev.properties
currency-service.url=http://dev.in28minutes.com
currency-service.username=devusername
currency-service.key=devkey
In application.properties:
spring.profiles.active=dev
✅ 6. Summary Table
Feature Purpose
1. Install Java
2. Install a Web/Application Server (like Tomcat, WebLogic, or WebSphere)
3. Deploy the application WAR file to the server
Issues:
spring-boot-starter-web includes:
o spring-boot-starter-tomcat (default embedded server)
So, when you add spring-boot-starter-web, Tomcat comes inside the JAR.
Works across environments Needs manual setup Just run the JAR
🔊 Popular Quote
Tomcat (Default)
Jetty
Undertow
With Spring Boot, deployment becomes as easy as copying and running a single file.
Perfect for microservices and modern DevOps!
Spring Boot Actuator (Monitoring Feature) - Notes
Jab hamari application production (live) environment me chalti hai, tab humein yeh
dekhna hota hai ki:
Yeh sab monitor karne ke liye Spring Boot Actuator ka use hota hai. Yeh Spring Boot
ka ek feature hai jo application ke status aur internals ko dekhne ke liye monitoring
endpoints deta hai.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2. Application run karo jaise normal Spring Boot app run karte hain.
📃 Default Behavior:
Jab aap application run karte ho aur browser me open karte ho:
http://localhost:8080/actuator
management.endpoints.web.exposure.include=*
1. http://localhost:8080/actuator/metrics
o Yeh page batayega ki app me kaun kaun se metrics available hain.
2. http://localhost:8080/actuator/metrics/http.server.requests
o Yeh dikhaega ki kitni HTTP requests aayi hain, unka total time kya tha,
etc.
Agar aap browser me 10 baar refresh karte ho, toh COUNT value badh jaayegi.
⚠️Important Note:
management.endpoints.web.exposure.include=health,metrics
🔄 Summary:
Spring Boot Actuator se aap apni app ko monitor kar sakte ho.
Health, beans, config, env, metrics jaise endpoints se aapko sab data milta hai.
Deployment ke baad app ka status dekhna asaan ho jaata hai.
Simple config se Actuator enable hota hai.
Yeh ek powerful tool hai jo kisi bhi real-world Spring Boot application me use hota hai
monitoring ke liye.
🔄 Spring Boot vs Spring MVC vs Spring Framework
🔹 1. Spring Framework kya hai?
Feature Description
Spring apne aap sab classes ko scan karta hai aur samajh jaata hai kis-
🔍 Component Scan
kis ko run karna hai
Spring MVC ek Spring ka module hai jo sirf Web Application aur REST API banane
ke liye hota hai.
Feature Description
🔧 Spring Framework pe
Ye bhi dependency injection use karta hai
based
Struts se better hai kyunki Struts me sab manually configure karna padta tha.
🔹 3. Spring Boot kya hai?
Spring Boot ek Spring Project hai jo Spring Framework + Spring MVC ko use karna
asaan banata hai.
Feature Explanation
⚙️Auto Configuration Automatically sab kuch configure kar deta hai – web, JPA, Tomcat
Tomcat already JAR ke andar hota hai – alag se install nahi karna
🧱 Embedded Server
padta
✅ Summary:
Term Meaning
Spring
Core engine – dependency injection aur bean configuration ke liye
Framework
🧐 Final Line:
Spring Boot ek wrapper hai jo Spring aur Spring MVC ka use asaani se aur fast karne
ke liye banaya gaya hai.
Ye unka competitor nahi, helper hai.
Apni Spring Boot web app me ek URL /say-hello banana jo browser me simple text
dikhaye:
"Hello! What are you learning today?"
java
CopyEdit
public String sayHello() {
return "Hello! What are you learning today?";
}
Ye method ek string return karta hai – wohi message jo hum browser me dikhana chahte
hain.
Spring ko batana hota hai ki kis URL pe ye method chalega. Uske liye:
java
CopyEdit
@RequestMapping("say-hello")
Spring tabhi method ko use karega jab us class ko woh manage kar raha ho. Uske liye
tumne class ke upar likha:
java
CopyEdit
@Controller
Jab tumne return "Hello..."; diya, to Spring MVC sochta hai ki yeh koi
HTML view name hai.
Wo dhoondhne lagta hai HTML file jiska naam hai Hello! What are you
learning today? (which obviously doesn't exist).
Result: ❌ 404 error (Not Found)
java
CopyEdit
@ResponseBody
Iska matlab: Return jo string diya hai, usko directly browser me dikhado, koi view
file mat dhoondo.
bash
CopyEdit
http://localhost:8080/say-hello