What is dependency injection?

We, as a java programmers rely on low coupling of the java components/classes. This aims at increasing the re-usability of the components and also help in independent testing of the component. To supplement to this feature, Spring came into picture and through its dependency injection capability, it helped the dependencies of a class to be automatically injected to the dependent class, without the effort of the dependent class finding or creating the same.

The prime concept behind the Dependency Injection is that a class should not be configured by itself, rather it should be configured from outside.

To make it simple, suppose there is a class A, which has a dependency to class B. Class A uses class B as a variable. Now when the dependency injection is applied, class B is given to the class A using two possible approaches:


  1. Constructor Injection
  2. Setter Injection

However there are two other types of injections too, namely:

  1. Interface Injection
  2. Lookup-method Injection

Dependency Injection also known as the Inversion of Control(IoC), is the most basic thing that Spring provides. It provides with a light weight container for Dependency Injection known as Spring core container.

Spring core container provides all the required functionality to the Spring framework. BeanFactory is the primary component of the Spring core container. The BeanFactory helps the core container in creating the new objects, which are generally created as Singletons if not specified differently. 

Spring actually comes with two types of containers:

  1. BeanFactory interface
  2. ApplicationContext interface

BeanFactory interface provides an advanced configuration mechanism and is capable of managing objects of any nature.

ApplicationContext interface on the other hand is a sub-interface of BeanFactory (or we can say, it’s built on top of BeanFactory, thus provides additional enterprise-centric features and functionalities than that of the BeanFactory) and allows easier integration with Spring’s AOP feature, internationalization, etc.

Kindly put on your queries as comments to this post. In the next post on Spring, I will be providing the demo on various dependency injection approaches as mentioned.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.