DEV Community

meenachan
meenachan

Posted on

Once Upon a Code Adventure: IoC and DI in the Land of Spring

Hello👋✨, little coders! Today, let's dive into a magical coding story where we meet two special friends: IoC (Inversion of Control) and DI (Dependency Injection). Picture it like a fairy tale where coding toys come to life in the Spring kingdom.

What Is Inversion of Control?

Imagine you have a magical friend named IoC. Normally, you decide which toys to play with and when. But IoC flips the story! Now, IoC decides when it's playtime and how the toys play together. It's like a magical friend taking charge of the toy party schedule.

Magic Words: Decoupling, Switching Toys Easily, Neat Toy Rooms, Testing Toys Easily

How We Achieve This Magic:

Strategy: Planning playtime strategies.
Service Locator: Asking a special friend where toys are.
Factory: A magical factory creating toys.
Dependency Injection (DI): Toys are given as gifts!

What Is Dependency Injection?

Now, let's talk about the magic of Dependency Injection (DI). Imagine your mom or dad giving you a special toy as a gift. DI is like receiving toys without making them yourself. Someone injects, or gives, toys into your toy box for you to play with.

Magic Words: Getting Toy Gifts, No Toy-Making Stress, Playful Assembly

Storytime in Code:

// Traditional Way: Making a toy
public class Store {
    private Item item;

    public Store() {
        item = new ItemImpl1();
    }
}

// Magic Way: Receiving a toy gift
public class Store {
    private Item item;

    public Store(Item item) {
        this.item = item;
    }
}
Enter fullscreen mode Exit fullscreen mode

The Spring Toy Kingdom

In the Spring kingdom, there's a special toy organizer called the IoC Container. It's like the king of toys, managing everything from creating to organizing the toys (beans). The Spring kingdom has different areas for toys (ApplicationContexts): one for regular toys, one for toys that like to play alone, and one for web toys!

Magical Words: ApplicationContext, King of Toys, Magical Toy Rooms

How Toys Are Assembled:

// Manually creating a toy room
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Enter fullscreen mode Exit fullscreen mode

Constructor-Based Dependency Injection

Now, let's talk about Constructor-Based Dependency Injection. It's like getting toys with specific instructions on how to play.

Magic Words: Magic Instructions, Special Play Plans

Storytime in Code:

// Magic Instructions in Code
@Configuration
public class ToyPlans {

    @Bean
    public Item item1() {
        return new ItemImpl1();
    }

    @Bean
    public Store store() {
        return new Store(item1());
    }
}
Enter fullscreen mode Exit fullscreen mode

Setter-Based Dependency Injection

Next up is Setter-Based Dependency Injection. It's like getting toys and deciding how to play with them later.

Magic Words: Play Plans Can Change, Decorating Toys Later

Storytime in Code:

// Changing play plans later
@Bean
public Store store() {
    Store store = new Store();
    store.setItem(item1());
    return store;
}
Enter fullscreen mode Exit fullscreen mode

Field-Based Dependency Injection

Now, let's talk about Field-Based Dependency Injection. It's like placing toys directly where they need to be.

Magic Words: Toys Find Their Spots

Storytime in Code:

// Placing toys in their spots
public class Store {
    @Autowired
    private Item item; 
}
Enter fullscreen mode Exit fullscreen mode

Autowiring Toys

Autowiring is like letting the toy room organizer (Spring) decide how toys connect.

Magic Words: Organized Toy Connections

Storytime in Code:

// Toy room organizer deciding connections
public class ToyPlans {
    @Bean(autowire = Autowire.BY_TYPE)
    public Store store() {
        return new Store();
    }
}
Enter fullscreen mode Exit fullscreen mode

Lazy Initialized Toys

Lastly, Lazy Initialized Toys are like toys that wake up only when you want to play with them.

Magic Words: Sleeping Toys, Surprise Playtime

Storytime in Code:

// Sleeping toys until playtime
<bean id="item1" class="store.ItemImpl1" lazy-init="true" />
Enter fullscreen mode Exit fullscreen mode

And that, little coders, is our magical coding adventure with IoC and DI in the Spring kingdom! May your coding toy room always be filled with joy and surprises! 🚀✨

Top comments (0)