Spring IoC Container » grokonez

https://grokonez.com/spring-framework/spring-ioc-container

Spring IoC Container

At the Core of Spring Framework, Spring IoC (Inversion of Control) is implemented using the Dependency Injection design pattern. The Spring IoC Container creates, configures and connects the objects (which are called Beans), then manage their life cycle.

This tutorial introduce principle of Spring IoC and a simple example using Containers to you.

I. Technology

- Java 1.8 - Maven 3.3.9 - Spring Tool Suite – Version 3.8.1.RELEASE - Spring Boot: 1.4.0.RELEASE

II. Overview

1. Container & Dependency Injection

The container helps inject the dependencies. Let's see an example: We want to provide a CustomerService with 2 implementations of CustomerPackage: BasicPackage and BusinessPackage. ioc-coupled This is content of CustomerService.java

public class CustomerService {
	private CustomerPackage customerPackage = new BasicPackage(); //or new BusinessPackage()

	public String callSupport() {
		return customerPackage.support();
	}
}

Because we create a CustomerPackage instance in the class CustomerService, it is tightly coupled. So if we want to change BasicPackage to BusinessPackage, we need to modify:


private CustomerPackage customerPackage = new BusinessPackage();

To avoid this kind of coupling, Spring Framework has a strong container to manage components. Instead of hard coding by creating new instance inside, we have the container inject the dependencies, by using setter injection or constructor injection.
Content of CustomerService.java after using Dependency Injection

More at:

https://grokonez.com/spring-framework/spring-ioc-container

Spring IoC Container

#springframework #springioc #container

Spring IoC Container » grokonez