Skip to main content

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:

  1. βœ… Explain what Inversion of Control (IoC) is
  2. βœ… Master the 3 types of dependency injection
  3. βœ… Create and manage Spring Beans
  4. βœ… Use appropriate scopes (@Singleton, @Prototype, etc.)
  5. βœ… Configure Spring with @Configuration and @Bean
  6. βœ… 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​

TypeDuration
Theory2h45 (4 chapters)
Practice4h30 (3 Labs)
Total7h15 (~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​

  1. Read the 4 theoretical chapters in order

    • Do not skip any chapter
    • Take notes
    • Test small examples
  2. Do the labs in order

    • Lab 5.1: Injection
    • Lab 5.2: Beans and Scopes
    • Lab 5.3: Configuration
  3. Review the summary of each chapter


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:

  1. Do you know what IoC means? ☐ Yes ☐ No
  2. Do you know the 3 types of injection? ☐ Yes ☐ No
  3. Do you understand the difference between @Component and @Bean? ☐ Yes ☐ No
  4. 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


⬅️ Back to Course Outline