Spring-DI-vs-IOC

Inversion of Control vs Dependency Injection

In software engineering, inversion of control (IoC) could also be programming technique during which object coupling is bound at run time by an assembler object and is is sometimes not known at compile time using static analysis.

In this spring tutorial, learn the difference between ioc and dependency injection in spring.

1. What is Dependency Injection (DI)

IoC is a design paradigm with the goal of giving more control to the targeted components of your application.

While Dependency injection could also be pattern want to make instances of objects that other objects believe without knowing at compile time which class are going to be used to provide that functionality.

IoC relies on dependency injection because a mechanism is required to activate the components providing the precise functionality.

Major benefit:

  1. flexible
  2. reusable
  3. encapsulated

2. What is Inversion of Control (IoC)

In traditional programming, the flow of the business logic is set by objects that are statically assigned to a minimum of each other. With inversion of control, the flow depends on the thing graph that’s instantiated by the assembler and is made possible by object interactions being defined through abstractions.
The binding process is achieved through dependency injection, although some argue that the utilization of a service locator also provides inversion of control.

Inversion of control as a design guideline serves the subsequent purposes:

There is a decoupling of the execution of a particular task from implementation.
Every module can focus on what it’s designed for.
Modules make no assumptions about what other systems do but believe their contracts.
Replacing modules has no side effect on other modules.

3. How to implement IoC

In object-oriented programming, there are several basic techniques to implement inversion of control. These are:

  • factory pattern
  • service locator pattern
  • using a dependency injection of any given below type:
    • constructor injection
    • a setter injection
    • an interface injection

4. Inversion of control in Spring

The org.springframework.beans and org.springframework.context packages provide the concept for the Spring Framework’s IoC container. The BeanFactory interface provides a sophisticated configuration mechanism capable of managing objects of any nature. The ApplicationContext interface builds on top of the BeanFactory (it could also be a sub-interface) and adds other functionality like easier integration with Spring’s AOP features, message resource handling (for use in internationalization), event propagation, and application-layer specific contexts like the WebApplicationContext to be used in web applications.

The BeanFactory is that the actual representation of the Spring IoC container that’s liable for containing and otherwise managing the aforementioned beans. The BeanFactory interface is that the central IoC container interface in Spring.

There are variety of implementations of the BeanFactory interface. The most commonly used BeanFactory implementation is that the XmlBeanFactory class. Other commonly used class is XmlWebApplicationContext. Depending on the bean definition, the factory will return either an independent instance of a contained object (the Prototype design pattern), or one shared instance (a superior alternative to the Singleton design pattern, in which the instance is a singleton in the scope of the factory). Which type of instance are going to be returned depends on the bean factory configuration: the API is that the same.

Before we dive into dependency injection types, let first identify the ways of making a bean in spring framework because it will help in understanding the items in next section.

5. How to create beans in Spring

A bean definition are often seen as a recipe for creating one or more actual objects. The container looks at the recipe for a named bean when asked, and uses the configuration metadata encapsulated by that bean definition to make (or acquire) an actual object.

5.1. Using constructor

When creating a bean using the constructor approach, all normal classes are usable by and compatible with Spring. That is, the category being created doesn’t get to implement any specific interfaces or be coded during a specific fashion. Just specifying the bean class should be enough. When using XML-based configuration metadata you’ll specify your bean class like so:

<bean id="exampleBean"/>

5.2. Using static factory method

When defining a bean which is to be created employing a static factory method, alongside the category attribute which specifies the category containing the static factory method, another attribute named factory-method is required to specify the name of the factory method itself.

Spring expects to be ready to call this method and obtain back a live object, which from that time on is treated as if it had been created normally via a constructor.

<bean id="exampleBean" factory-method="createCustomInstance"/>

5.3. Using instance factory method

In a fashion almost like instantiation via a static factory method, instantiation using an instance factory method is where the factory method of an existing bean from the container is invoked to create the new bean.

<bean id="myCustomFactoryBean" class="...">
<bean id="exampleBean" factory-bean="myCustomFactoryBean" factory-method="createInstance"></bean>

6. Dependency Injection in Spring

The basic principle behind Dependency Injection (DI) is that objects define their dependencies only through constructor arguments, arguments to a factory method, or properties which are assailed the thing instance after it’s been constructed or returned from a factory method. Then, it’s the work of the container to truly inject those dependencies when it creates the bean. This is fundamentally the inverse, hence the name Inversion of Control (IoC).

6.1. Setter Injection

Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

public class OnlineTutorialsTechSetterDIExample {

ExampleBean exampleBean = null;
public void setExampleDemoBean(ExampleBean exampleBean) {
this.exampleBean = exampleBean;
}
}

6.2. Constructor Injection

Constructor-based DI is realized by invoking a constructor with variety of arguments, each representing a collaborator. Additionally, calling a static factory method with specific arguments to construct the bean, are often considered almost equivalent, and therefore the remainder of this text will consider arguments to a constructor and arguments to a static factory method similarly.

public class OnlineTutorialsConstructorDIExample {

ExampleBean exampleBean = null;

public OnlineTutorialsConstructorDIExample (ExampleBean exampleBean) {
this.exampleBean = exampleBean;
}
}

spring ioc vs di

Reference:

%d bloggers like this: