0% found this document useful (0 votes)
4 views3 pages

SpringBoot_Topic6_DI_BeanManagement

The document explains Dependency Injection (DI) in Spring Boot, detailing its definition and types: Constructor DI, Setter DI, and Field Injection. It also introduces Spring Beans, which are objects managed by the Spring container, and provides examples of bean declaration using annotations like @Component, @Service, and @Repository. Additionally, it includes common interview questions related to DI and Spring Beans.

Uploaded by

shivamsitu2
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)
4 views3 pages

SpringBoot_Topic6_DI_BeanManagement

The document explains Dependency Injection (DI) in Spring Boot, detailing its definition and types: Constructor DI, Setter DI, and Field Injection. It also introduces Spring Beans, which are objects managed by the Spring container, and provides examples of bean declaration using annotations like @Component, @Service, and @Repository. Additionally, it includes common interview questions related to DI and Spring Beans.

Uploaded by

shivamsitu2
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
You are on page 1/ 3

Spring Boot - Dependency Injection (DI) & Bean Management

Topic 6: Spring Boot - Dependency Injection (DI) & Bean Management

What is Dependency Injection (DI)?

Dependency Injection is a design pattern where the dependencies (objects a class needs) are provided by an

external source rather than the class creating them.

Types of DI in Spring:

1. Constructor DI - Inject dependency via constructor (@Autowired)

2. Setter DI - Inject dependency via setter method (@Autowired)

3. Field Injection - Inject directly into class field (@Autowired)

Example: Constructor-Based DI

@Service

public class MyService {

public String getData() {

return "Spring Boot DI!";

@RestController

public class MyController {

private final MyService myService;

@Autowired

public MyController(MyService myService) {

this.myService = myService;

@GetMapping("/data")

public String fetchData() {


Spring Boot - Dependency Injection (DI) & Bean Management
return myService.getData();

Spring Beans:

A bean is an object that is managed by the Spring container.

Declaring a Bean:

- @Component

- @Service

- @Repository

- @Controller

- @Bean (manual declaration)

Example: Manual Bean Declaration

@Configuration

public class AppConfig {

@Bean

public MyService myService() {

return new MyService();

Interview Questions:

Q1: What is Dependency Injection?

A: DI is a design pattern in which a class receives its dependencies from an external source.

Q2: Ways to inject dependencies in Spring?

A: Constructor, Setter, and Field injection.


Spring Boot - Dependency Injection (DI) & Bean Management
Q3: Difference between @Component, @Service, and @Repository?

A:

- @Component: Generic stereotype.

- @Service: Used in service layer.

- @Repository: Used in data layer with exception translation.

Q4: What is a Spring Bean?

A: A Spring Bean is an object managed by the Spring IoC container.

You might also like