0% found this document useful (0 votes)
0 views7 pages

Notes 01

The document provides an overview of the Spring Framework, focusing on Dependency Injection (DI) and its types, including Constructor, Setter, and Field Injection. It also outlines important Spring annotations such as @Component, @Service, @Repository, @Controller, and @RestController, along with their definitions and examples. Additionally, it covers key interview questions related to Spring, including IoC, bean lifecycle, and autowiring.

Uploaded by

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

Notes 01

The document provides an overview of the Spring Framework, focusing on Dependency Injection (DI) and its types, including Constructor, Setter, and Field Injection. It also outlines important Spring annotations such as @Component, @Service, @Repository, @Controller, and @RestController, along with their definitions and examples. Additionally, it covers key interview questions related to Spring, including IoC, bean lifecycle, and autowiring.

Uploaded by

764t59r4t5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🚀 PART 1: SPRING CORE (VERY IMPORTANT FOR INTERVIEW)

🔹 WHAT IS SPRING FRAMEWORK?


👉 A lightweight framework used to build Java applications with loose coupling
and dependency injection.

🔥 DEPENDENCY INJECTION (DI)


🔹 DEFINITION
👉 Dependency Injection is a design pattern where objects are provided by the
Spring container instead of creating them manually.

🔹 WHY DI?
Loose coupling
Easy testing
Reusability

🧩 TYPES OF DEPENDENCY INJECTION


1️⃣ CONSTRUCTOR INJECTION
🔹 DEFINITION
👉 Dependencies are injected through the constructor.
🔹 EXAMPLE
@Component
class Engine {}

@Component
class Car {

Engine engine;

public Car(Engine engine) {


[Link] = engine;
}
}

2️⃣ SETTER INJECTION


🔹 DEFINITION
👉 Dependencies are injected using setter methods.
🔹 EXAMPLE
@Component
class Car {

Engine engine;

@Autowired
public void setEngine(Engine engine) {
[Link] = engine;
}
}

3️⃣ FIELD INJECTION (MOST COMMON BUT NOT RECOMMENDED IN


INTERVIEWS)
🔹 DEFINITION
👉 Dependency is injected directly into field.
🔹 EXAMPLE
@Component
class Car {

@Autowired
Engine engine;
}

⚠️ Interview Tip:
👉 Always say:
✔ Constructor Injection is best practice

🧠 SPRING BEAN
🔹 DEFINITION
👉 A Bean is an object managed by the Spring container.
🏷️ IMPORTANT SPRING ANNOTATIONS (WITH
DEFINITIONS + EXAMPLES)

🔹 @COMPONENT
DEFINITION
👉 Marks a class as a Spring Bean.
EXAMPLE

@Component
class Engine {}

🔹 @SERVICE
DEFINITION
👉 Used for business logic layer.
EXAMPLE

@Service
class UserService {
public void saveUser() {}
}

🔹 @REPOSITORY
DEFINITION
👉 Used for database layer and handles exceptions.
EXAMPLE

@Repository
class UserRepository {
public void save() {}
}

🔹 @CONTROLLER
DEFINITION
👉 Handles web requests (MVC).
EXAMPLE

@Controller
class MyController {

@RequestMapping("/home")
public String home() {
return "[Link]";
}
}

🔹 @RESTCONTROLLER
DEFINITION
👉 Used to create REST APIs (returns JSON/text).
EXAMPLE

@RestController
class ApiController {

@GetMapping("/api")
public String getData() {
return "Data";
}
}

🔹 @AUTOWIRED
DEFINITION
👉 Automatically injects dependency.
EXAMPLE

@Autowired
Engine engine;

🔹 @QUALIFIER
DEFINITION
👉 Used when multiple beans exist to specify which one to inject.
EXAMPLE
@Autowired
@Qualifier("dieselEngine")
Engine engine;

🔹 @SPRINGBOOTAPPLICATION
DEFINITION
👉 Main configuration annotation (combines 3 annotations).
EXAMPLE

@SpringBootApplication
public class App {
public static void main(String[] args) {
[Link]([Link], args);
}
}

🚀 PART 2: SPRING INTERVIEW QUESTIONS (WITH


ANSWERS)

❓ WHAT IS IOC (INVERSION OF CONTROL)?


👉 The control of object creation is transferred to Spring container.
❓ WHAT IS BEAN LIFECYCLE?
👉 Steps:
1. Bean creation
2. Dependency injection
3. init()
4. ready to use
5. destroy()

❓ DIFFERENCE: @COMPONENT VS @SERVICE VS @REPOSITORY?


👉 All are same but used for different layers:
Annotation Use
@Component General

@Service Business logic

@Repository Database

❓ WHAT IS SPRING CONTAINER?


👉 It manages beans and dependency injection.
❓ WHAT IS AUTOWIRING?
👉 Automatic dependency injection by Spring.
❓ WHAT IS @REQUESTMAPPING?
👉 Maps URL to method.
❓ WHAT IS @GETMAPPING / @POSTMAPPING?
👉 Shortcut for HTTP methods.
❓ WHAT IS SPRING BOOT STARTER?
👉 Pre-configured dependency package.
🧠 MINI REAL EXAMPLE (IMPORTANT)
@Service
class UserService {
public String getUser() {
return "User Data";
}
}

@RestController
class UserController {

@Autowired
UserService service;

@GetMapping("/user")
public String user() {
return [Link]();
}

🎯 FINAL TRAINER SUMMARY


Now you understand:

✅ DI (3 types)
✅ Bean & lifecycle
✅ All important annotations
✅ Real usage in project

You might also like