🌱 Spring Framework – Key Concepts & Annotations
1️⃣ Common Annotations in Spring
Annotation | Description | Example |
---|---|---|
@Component | Marks a class as a Spring-managed bean (added to IoC container). You can give it a custom name. | @Component("nService") |
@Autowired | Automatically injects a required dependency. | @Autowired NotificationService service; |
@Qualifier | Used when multiple beans of the same type exist — specifies which bean to inject. | @Qualifier("airIndia") |
@primary | Marks one bean as the default choice when multiple candidates exist. | @Primary |
@scope | Defines bean scope (singleton , prototype , etc.). |
@Scope("prototype") |
@PostConstruct | Method runs after dependency injection is completed (bean initialization). | @PostConstruct public void init() { ... } |
@PreDestroy | Method runs before bean destruction (for cleanup). | @PreDestroy public void cleanup() { ... } |
🧩 Note: To use
@PreDestroy
and@PostConstruct
, include the dependency:
javax.annotation-api
2️⃣ Dependency Injection (DI) Concept
Spring uses the IoC (Inversion of Control) Container to manage bean creation and injection.
Why Constructor Injection is Preferred:
- Ensures all required dependencies are available when creating a bean.
- Promotes immutability and better testability.
- Prevents
null
dependencies during initialization.
Example:
@Component
public class NotificationService {
private final SMSGateway smsGateway;
@Autowired
public NotificationService(SMSGateway smsGateway) {
this.smsGateway = smsGateway;
}
}
Here, when NotificationService
is created, Spring automatically looks for a SMSGateway
bean and injects it.
If it doesn’t exist, Spring will create it (if it’s annotated with @Component
).
3️⃣ Multiple Bean Scenario – @Qualifier & @primary
public interface FlightService {
void bookFlight();
}
@Component
@Qualifier("indigoService")
class IndigoFlightService implements FlightService {
public void bookFlight() {
System.out.println("Booking Indigo flight...");
}
}
@Component
@Primary
class AirIndiaFlightService implements FlightService {
public void bookFlight() {
System.out.println("Booking Air India flight...");
}
}
@Component
class TravelController {
@Autowired
private FlightService flightService; // AirIndiaFlightService is injected (as @Primary)
}
✅ @primary makes
AirIndiaFlightService
the default choice.
🧭 Use @Qualifier if you want to explicitly choose another bean.
4️⃣ Spring Boot Overview
-
Simplifies Spring setup through:
- Auto-configuration
- Embedded servers (e.g., Tomcat)
-
Starter dependencies (e.g.,
spring-boot-starter-web
,spring-boot-starter-data-jpa
)
Removes the need for XML configuration.
Provides production-ready features like logging, health checks, and metrics.
5️⃣ Spring MVC Overview
MVC Pattern = Model – View – Controller
Component | Description |
---|---|
Model | Represents data and business logic |
View | UI representation (HTML, JSON, etc.) |
Controller | Handles requests and sends responses |
Common Annotations:
-
@Controller
/@RestController
-
@RequestMapping
,@GetMapping
,@PostMapping
-
@RequestParam
,@PathVariable
,@RequestBody
,@ResponseBody
6️⃣ Logging in Spring Boot
Spring Boot uses SLF4J + Logback by default.
Configuration in application.properties
:
logging.level.root=INFO
logging.level.com.example=DEBUG
logging.file.name=app.log
Log Levels: TRACE
, DEBUG
, INFO
, WARN
, ERROR
Summary
Concept | Key Takeaway |
---|---|
IoC & DI | Spring manages bean creation and injection automatically. |
Constructor Injection | Ensures dependencies are available early and promotes immutability. |
@Qualifier / @primary | Handle multiple bean injection conflicts. |
@scope | Controls bean lifecycle (singleton/prototype). |
Spring Boot | Reduces boilerplate, adds auto-configuration & embedded server. |
Spring MVC | Follows Model–View–Controller pattern for web applications. |
Logging | Built-in via SLF4J and Logback with configurable levels. |
Top comments (0)