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

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:

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

Now, we don’t need to instantiate CustomerPackage object because the container will handle the operation.
ioc-loosecoupled

Configuration for IoC Container will be done in XML file:

The container reads configuration metadata (XML, Java annotations, or Java code) to know what objects to instantiate, configure, and assemble.
The diagram below shows a high-level view of how Spring Container works:
iocdiagram

2. Types of Containers

Spring provides two types of containers:
BeanFactory: the root interface of Spring IoC container.
ApplicationContext: child interface of BeanFactory, provides Spring AOP features. It has some useful implementations:
+ AnnotationConfigApplicationContext: for standalone java applications and in case of using annotations for Configuration.
+ ClassPathXmlApplicationContext: If using Configuration XML File in standalone application, we do not need to provide the full path of the XML file.
+ FileSystemXmlApplicationContext: This is similar to ClassPathXmlApplicationContext but Configuration XML File can be loaded from anywhere in the file system, so we must provide full path.
+ AnnotationConfigWebApplicationContext and XmlWebApplicationContext: for web applications.

3. Project Structure

ioc-project-structure

4. Step to do

– Create Spring Boot project
– Create Interface & Classes for Bean
– Create Service
– Create Spring Bean Configuration File
– Add commands to SpringBootApplication Class
– Run Spring Boot Application & Enjoy Result

III. Practice
1. Create Spring Boot project

Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields:
ioc-startproj

Click Next and Finish. Spring Boot project will be created successfully.

2. Create Interface & Classes for Bean

Under package bean, create interface CustomerPackage and its implementations:

CustomerPackage.java

BasicPackage.java

BusinessPackage.java

3. Create Service

Under package service, create CustomerService.java:

4. Create Spring Bean Configuration File

Under src/main/resources, create bean.xml:

5. Add commands to SpringBootApplication Class

Open SpringIoCApplication.java, change the content inside:

6. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results in Console Screen:

IV. Source Code

springioc


Related Posts



Got Something To Say:

Your email address will not be published. Required fields are marked *

*