MODULE 5 - Spring Core: Advanced IoC and DI
Welcome to Module 5! π―β
You have used dependency injection in previous modules, but do you really understand how it works?
This module reveals the inner workings of Spring and empowers you to handle any dependency injection situation.
π Learning Objectivesβ
By the end of this module, you will be able to:
- β Explain what Inversion of Control (IoC) is
- β Master the 3 types of dependency injection
- β Create and manage Spring Beans
- β Use appropriate scopes (@Singleton, @Prototype, etc.)
- β Configure Spring with @Configuration and @Bean
- β Manage profiles (dev, test, prod)
π Prerequisitesβ
Before starting this module, you should have:
- β Completed Modules 1 to 4
- β Used constructor injection in your labs
- β Understood the 3-layer architecture (Controller, Service, Repository)
If these prerequisites are not met, return to the previous modules!
πΊοΈ Module Outlineβ
Theoretical Part (4 chapters)β
Chapter 1: Inversion of Control (IoC)β
Duration: 30 minutes
Content:
- What is IoC?
- The problem of tight coupling
- How Spring solves this problem
- The Spring Container (ApplicationContext)
Why important? Understand the fundamental principle of Spring
Chapter 2: Dependency Injection (DI)β
Duration: 45 minutes
Content:
- The 3 types of injection (constructor, setter, field)
- Why constructor injection is preferred
- @Autowired, @Inject, @Resource
- Optional dependencies
Why important? Choose the right injection method
Chapter 3: Beans and Spring Contextβ
Duration: 45 minutes
Content:
- What is a Bean?
- @Component, @Service, @Repository, @Controller
- @Bean and @Configuration
- Bean naming
- @Qualifier and @Primary
Why important? Master the creation and management of Spring objects
Chapter 4: Scopes and Configurationβ
Duration: 45 minutes
Content:
- Scopes: @Singleton, @Prototype
- Web scopes: @RequestScope, @SessionScope
- Java vs XML configuration
- Profiles (@Profile) for dev/test/prod
- @ComponentScan
Why important? Control lifecycle and configuration
Practical Part (3 Labs)β
Lab 5.1: Advanced Dependency Injectionβ
Duration: 1h30
Objective: Master the different types of injection
You will create:
- Multiple services with different injection methods
- Manage circular dependencies
- Use @Qualifier to choose the implementation
Lab 5.2: Beans and Scopesβ
Duration: 1h30
Objective: Understand scopes and their impact
You will create:
- Beans with different scopes
- Test @Singleton vs @Prototype
- Create custom beans with @Bean
Lab 5.3: Profile-based Configurationβ
Duration: 1h30
Objective: Manage multiple environments
You will create:
- Configuration for dev, test, prod
- Conditional beans based on profile
- application-{profile}.properties
π Total Durationβ
| Type | Duration |
|---|---|
| Theory | 2h45 (4 chapters) |
| Practice | 4h30 (3 Labs) |
| Total | 7h15 (~2-3 sessions) |
π― What You Will Understandβ
Before this Moduleβ
// You wrote this without really understanding:
@Service
public class MyService {
private final MyRepository repo;
public MyService(MyRepository repo) {
this.repo = repo;
}
}
Unanswered questions:
- Why @Service?
- How does Spring know it should create MyRepository?
- Why constructor injection?
- When are these objects created?
After this Moduleβ
// You will understand EVERYTHING:
@Service // β Spring manages this bean (singleton by default)
public class MyService {
private final MyRepository repo; // β Immutable (best practice)
// β Constructor injection (recommended)
public MyService(MyRepository repo) {
this.repo = repo; // β Spring injects automatically
}
}
You will know:
- β How Spring creates and manages objects
- β When to use @Component, @Service, @Repository
- β How to manage multiple implementations
- β How to configure for different environments
- β The different scopes and their usefulness
π Why This Module Is Importantβ
1. Understand the Foundationsβ
Spring Boot "hides" a lot of complexity with its auto-configuration. It's great for quick starts, but you need to understand what's happening "under the hood."
Concrete example:
Common error: "No qualifying bean of type X found"
Without this module: π° You search on StackOverflow
With this module: π You understand and fix the problem in 2 minutes
2. Write Professional Codeβ
Junior developers copy code without understanding.
Senior developers understand and adapt according to the context.
This module takes you from junior to senior on Spring Core.
3. Interview Preparationβ
Questions on IoC, DI, and Spring beans are extremely common in technical interviews.
Typical questions:
- "Explain Inversion of Control"
- "What is the difference between @Component and @Bean?"
- "When to use @Qualifier?"
- "Explain bean scopes"
After this module, you will have all the answers!
πΊοΈ Progress in the Trainingβ
Module 1: Introduction to Spring Boot
Module 2: Getting Started (Initializr, Structure, Config)
Module 3: 3-Layer Architecture
Module 4: Production Databases
Module 5: β YOU ARE HERE - Spring Core (IoC/DI)
Understand the foundations
Module 6: Advanced Spring Data JPA
Module 7+: Advanced concepts
Module 5 = The moment you REALLY understand Spring!
π How to Approach This Moduleβ
Theory BEFORE Practiceβ
-
Read the 4 theoretical chapters in order
- Do not skip any chapter
- Take notes
- Test small examples
-
Do the labs in order
- Lab 5.1: Injection
- Lab 5.2: Beans and Scopes
- Lab 5.3: Configuration
-
Review the summary of each chapter
Recommended Timeβ
Intensive Option (1 weekend):
- Saturday morning: Chapters 1 and 2 + Lab 5.1
- Saturday afternoon: Chapter 3 + Lab 5.2
- Sunday morning: Chapter 4 + Lab 5.3
- Sunday afternoon: Review and exercises
Progressive Option (2 sessions):
- Session 1: Chapters 1-2 + Lab 1
- Session 2: Chapters 3-4 + Lab 5.2 and Lab 5.3
β Self-Assessmentβ
Before starting, answer:
- Do you know what IoC means? β Yes β No
- Do you know the 3 types of injection? β Yes β No
- Do you understand the difference between @Component and @Bean? β Yes β No
- Do you know what a bean scope is? β Yes β No
If you answered "No" to any question β This module is for you!
After the module, retake this test. You should answer "Yes" to EVERYTHING!
π― Next Stepβ
Ready to understand Spring in depth?
β‘οΈ Start with Chapter 1: Inversion of Control