0% found this document useful (0 votes)
3 views13 pages

Spring Boot Profiles Notes

Spring Boot Profiles enable developers to manage environment-specific configurations for applications using separate property files for each environment. The document explains how to set up profiles, create configuration properties, and utilize embedded servers for simplified deployment. Additionally, it covers the Spring Boot Actuator for monitoring application health and metrics, and compares Spring Boot with Spring MVC and the Spring Framework.

Uploaded by

jaysugave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Spring Boot Profiles Notes

Spring Boot Profiles enable developers to manage environment-specific configurations for applications using separate property files for each environment. The document explains how to set up profiles, create configuration properties, and utilize embedded servers for simplified deployment. Additionally, it covers the Spring Boot Actuator for monitoring application health and metrics, and compares Spring Boot with Spring MVC and the Spring Framework.

Uploaded by

jaysugave
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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:

 - In development: connect to dev-database.com


 - In production: connect to prod-database.com

Instead of changing code manually each time, you use profiles to manage environment-
specific settings.

2. Profiles in Spring Boot


You create separate configuration files for each environment:

 - 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.

- Values from profile-specific file override defaults.

5. Logging Levels
Logging Level Description

TRACE Logs everything (most detailed)


DEBUG Logs debugging info

INFO Logs general information

WARN Logs warnings

ERROR Logs only errors

OFF Disables logging

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.

Spring Boot Notes: ConfigurationProperties and Profiles (Simplified)

🔹 Topic: Configuration Properties + Profiles in Spring Boot

✅ 1. Real-Life Problem:

In real projects, we often have different environments:

 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:

Spring Boot provides:

▶ @ConfigurationProperties

 Helps you bind multiple related config values into one Java class.
 Recommended when you have a group of related settings.

▶ Profiles

 Helps switch between configurations for different environments.


 Example: application-dev.properties, application-prod.properties

✅ 3. Step-by-Step Guide:

▶ Step 1: Create a Java Class


@Component
@ConfigurationProperties(prefix = "currency-service")
public class CurrencyServiceConfiguration {
private String url;
private String username;
private String key;

// Generate Getters and Setters


}

▶ Step 2: Add properties in application.properties


currency-service.url=http://default.in28minutes.com
currency-service.username=defaultusername
currency-service.key=defaultkey

✅ 4. Create a Controller to Test It


@RestController
public class CurrencyConfigurationController {

@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

▶ Activate the profile

In application.properties:

spring.profiles.active=dev

Now, dev-specific values will be used.

✅ 6. Summary Table
Feature Purpose

application.properties Default config values

application-dev.properties Dev environment config

@ConfigurationProperties Bind config values to Java class

@Component Lets Spring manage the class as a Bean

@Autowired Injects the config class into controller

spring.profiles.active Sets which profile is active

This combination of @ConfigurationProperties + Profiles allows clean, environment-


specific, and scalable configuration handling in Spring Boot applications.
Spring Boot: Embedded Server (Simplified Notes)

🔧 Problem: Old Way of Deploying Java Apps (WAR Method)

Steps in traditional deployment:

1. Install Java
2. Install a Web/Application Server (like Tomcat, WebLogic, or WebSphere)
3. Deploy the application WAR file to the server

Issues:

 Complex setup process


 Need to repeat for every environment (Dev, QA, Staging, Prod)
 Slower deployments

✅ Spring Boot's Solution: Embedded Servers

Spring Boot simplifies this using an embedded server.

What does that mean?

 The server (like Tomcat) is packaged inside the JAR file.


 You only need Java installed on the machine.
 Run your application with one command:

java -jar your-spring-boot-app.jar

🚀 Example Workflow (From the Instructor)

1. Build the JAR file:


o Run mvn clean install
o Creates a JAR in target/ directory (e.g., learn-spring-boot-0.0.1-
SNAPSHOT.jar)
2. Run the application:
3. java -jar target/learn-spring-boot-0.0.1-SNAPSHOT.jar

Your app is live at http://localhost:8080


o
4. Fixing Port Issues:
o If Port 8080 is already in use, stop the other app first, then rerun.
📅 Dependencies That Enable Embedded Server

 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.

🔹 Benefits of Embedded Server


Spring Boot
Feature Traditional WAR Approach
Embedded JAR

Web server required? Yes No

Setup process Complex Simple

Deployment speed Slow Fast

Ideal for CI/CD No Yes

Works across environments Needs manual setup Just run the JAR

🔊 Popular Quote

"Make JAR, not WAR"


— Josh Long, Spring Developer Advocate

📊 Supported Embedded Servers

 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

Spring Boot Actuator (Monitoring Feature) - Notes

✨ Spring Boot Actuator kya hota hai?

Jab hamari application production (live) environment me chalti hai, tab humein yeh
dekhna hota hai ki:

 Kya application theek se chal rahi hai ya nahi?


 Kitni requests aa rahi hain?
 Application ke andar kya chal raha hai?

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.

✅ Actuator ko project me kaise add karein?

1. pom.xml me dependency add karo:

<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

Tab aapko sirf health endpoint dikhega.

 Health endpoint batata hai ki app "UP" hai ya nahi.


⚖️Extra Endpoints Enable Karna:
Zyada endpoints dekhne ke liye application.properties file me likho:

management.endpoints.web.exposure.include=*

Ab aapko multiple endpoints dikhenge jaise:

 /actuator/beans : app ke sare Spring beans


 /actuator/env : environment variables (port, Java version, encoding, etc.)
 /actuator/configprops : application.properties ke values
 /actuator/metrics : metrics jaise kitni HTTP requests aayi, memory usage, etc.
 /actuator/mappings : app ke sare URL mappings

🔹 Example - Metrics dekhna:

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:

 Production me sab endpoints expose karna safe nahi hota.


 Sirf zaroori endpoints enable karo jaise:

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?

Spring Framework ek core framework hai jo mainly 3 kaam karta hai:

Feature Description

Matlab ek object dusre object pe depend karta hai, Spring unhe


✅ Dependency
automatically connect kar deta hai (@Component, @Service,
Injection
@Autowired use karke)

Spring apne aap sab classes ko scan karta hai aur samajh jaata hai kis-
🔍 Component Scan
kis ko run karna hai

Spring automatically sab cheeze jodta hai (jaise controller me service


🔧 Auto-wiring
inject karna)

Lekin sirf dependency injection se app nahi banta!


Aapko database, testing, logging, sab chahiye hota hai.
Isiliye Spring ke modules aur projects aate hain, jaise:

 JPA/Hibernate support ke liye


 JUnit/Mockito for testing

🔹 2. Spring MVC kya hai?

Spring MVC ek Spring ka module hai jo sirf Web Application aur REST API banane
ke liye hota hai.

Feature Description

@Controller, @RestController, @RequestMapping use


🌐 Web banane ke liye
hote hain

🔁 REST APIs ka support Easily backend APIs ban jaati hain

🔧 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

Jaise spring-boot-starter-web, spring-boot-starter-data-


🚀 Starter Projects
jpa – ye sab dependencies automatically le aate hain

⚙️Auto Configuration Automatically sab kuch configure kar deta hai – web, JPA, Tomcat

Easily application.properties ya application-


🔍 Profiles/Properties
dev.properties se config ho jaata hai

📊 Actuator Monitoring ke liye endpoints deta hai

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

Spring MVC Web/REST app banane ke liye Spring Framework ka part

In dono ko easy banane ka tool – auto config, starter, embedded server,


Spring Boot
monitoring, error handling

🧐 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.

PROJECT AND LEARNINGS


✅ Goal of this Step:

Apni Spring Boot web app me ek URL /say-hello banana jo browser me simple text
dikhaye:
"Hello! What are you learning today?"

🧱 1. Naya Controller Class Banaya

 Tumne ek naya class banaya: SayHelloController


 Isko package me daala: com.in28minutes.springboot.myfirstwebapp.hello
 Iska kaam hoga browser se request lena aur response dena.

🎯 2. Simple Method Banaya

Tumne ek method banaya:

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.

🔗 3. Request Mapping Kiya

Spring ko batana hota hai ki kis URL pe ye method chalega. Uske liye:

java
CopyEdit
@RequestMapping("say-hello")

Matlab: Jab koi localhost:8080/say-hello pe jaayega, to ye method call hoga.

🧠 4. Spring Ko Controller Bataya

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

Isse Spring jaanta hai ki ye ek web controller hai.

⚠️5. Error Aayi – String View Samjha

 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)

✅ 6. Fix Kiya Using @ResponseBody

Tumne method ke upar likha:

java
CopyEdit
@ResponseBody

Iska matlab: Return jo string diya hai, usko directly browser me dikhado, koi view
file mat dhoondo.

🚀 7. App Run Kiya

Tumne app start kiya from class: MyfirstwebappApplication


Wahi class jisme public static void main() method hoti hai.

🌐 8. URL Pe Jaake Dekha

Tumne browser me URL type kiya:

bash
CopyEdit
http://localhost:8080/say-hello

Aur browser me message aaya:


sql
CopyEdit
Hello! What are you learning today?

🎉 TUMHARA PEHLA SPRING BOOT WEBPAGE KAAM KAR GAYA!

🔍 Troubleshooting Tips (Agar kaam na kare to):

1. @Controller laga hai ya nahi?


2. @RequestMapping("say-hello") sahi URL diya hai ya nahi?
3. @ResponseBody likha hai ya nahi?
4. sayHelloController class myfirstwebapp ke sub-package me hi hai ya nahi?
5. URL correctly likha hai? http://localhost:8080/say-hello

You might also like