Inversion of Control container & AOP in Spring

The Inversion of Control (IoC)

In objected oriented programming it all about to remove the dependencies from your code.

Tightly coupling

It means you directly reference and instantiates objects in your code. As a result, they’re dependent on each other and you will be in trouble when you would like to change these objects. Your code going to break at all references.

Example for tightly coupled reference:

public class CoffeeShop{
    private Coffee item;
  
    public CoffeeShop() {
        item = new DarkCoffee();    
    }
}

In the code above if you would like to change the type of Coffee you should change it inside the class. For the reason of that, if you have a lot of objects using it, you need the change it at every place. Hence, the class itself has to control their dependencies.

Inversion of Control container in Spring framework

Loose coupling

The goal is to achieve loose coupling, where the objects don’t depend on each other and you can change your references centrally without any trouble

Here is an example of how we can achieve it:

public class CoffeeShop{
    private Coffee item;
    public Coffee (Coffee item) {
        this.item = item;
    }
}

As you can see we got our object from outside and it can be any type of Coffee. Hence, if we would like to change is not necessary to touch the CoffeeShop class.

Inversion of Control is a programming pattern to achieve loose coupling where the independent objects don’t responsible for their dependencies. In contrast, they’re getting it from outside.

This is how we getting started to use the Inversion of Control pattern.

The Spring Inversion of Control container

It is the heart of the Spring Framework. The IoC container receives metadata from either an XML file, Java annotations, or Java code. The container gets its instructions on what objects to instantiate, configure, and assemble from simple Plain Old Java Objects (POJO) by reading the configuration metadata provided. These created objects through this process called Spring Beans. These objects are ready to Inject by Dependency Injection (DI).

The responsibilities of IoC container are:

  1. Instantiating the bean
  2. Wiring the beans together
  3. Configuring the beans
  4. Managing the bean’s entire life-cycle

Inversion of Control container in Spring framework

This diagram represents an abstract view of the working of Spring Framework. It shows how Spring makes use of Java POJO classes and configuration metadata.

More about the container from the official Spring site here.

What is Dependency Injection?

DI is the process of how we read objects from the IoC container and pass them to other objects. It has become one of the cornerstones of modern software engineering, as it is fundamental to allow proper testing.

What is Aspect-oriented programming?

Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects. Put it simple, it’s just an interceptor to intercept some processes, for example, when a method is execute, Spring AOP can hijack the executing method, and add extra functionality before or after the method execution.

In Spring AOP, 4 type of advices are supported :

  • Before advice – Run before the method execution
  • After returning advice – Run after the method returns a result
  • After throwing advice – Run after the method throws an exception
  • Around advice – Run around the method execution, combine all three advices above.

for more detail regarding please read: https://www.mkyong.com/spring/spring-aop-examples-advice/

Toufiq Mahmud