Dependency Injection: Concept of Spring Framework

Nandita Pandey
Analytics Vidhya
Published in
3 min readApr 7, 2020

--

overview of dependency injection

Spring framework came into limelight when a book named J2EE Development Without EJB by Rod Johnson was released in 2002. The author with his idea of spring framework thought it could be better than the Java EE. At that time Java Enterprise Edition was thought to be very heavyweight and also working with EJB was a difficult task to be done by a developer perspective. The author then came up with the spring framework and one of the major important concept of spring framework is the dependency injection.

An overview of Dependency Injection

Spring framework serves as a dependency injection, which is a form of inversion control ( Inversion of Control is a programming principle which inverts the flow of control as compared to traditional control flow). IoC is a kind of Hollywood principle, don’t call us, we’ll call you, which is applied to programming.

Inversion of Control Principle

The spring framework serving as a dependency injection binds together the classes of your application with the help of the instructions provided by you, which can be provided as annotations in the code or by explicitly binding the configuration code or by using an XML configuration.

Explanation using an example

If you start with an application, it might require other classes as well, like helper and utility classes. So, in basic Java the application class would instantiate the helper and utility classes and then start using them, which will increase the coupling between the classes. Now there’s a tight coupling between the classes, as the application class now depends upon the correct implementation of helper and utility classes and also there is no concept of abstraction among the classes. This makes it difficult to test the application class alone, as it will always instantiate the other two classes.

This is now when the spring dependency injection container comes into picture. With the help of spring dependency injection you will have the same classes to run your application but rather than instantiating and binding the classes, you will have to instruct the spring DI container to do the same for you. this can be done using annotations in the code or using configuration code or with an XML configuration code.

An example of spring DI container

In the spring framework we have to tell the spring about the classes that are being used in an application code and the inter dependencies of the classes on each other. The spring DI controller will instantiate the helper, utility and application classes, these instances will be called as spring beans.

Now that the DI container has been configured, it knows the dependencies among the classes of your application. But, without instantiating the helper and the utility classes from the application class the spring DI container will inject the helper and utility classes into the application class, by this the helper and the utility classes will be decoupled from the application class. Also the configuration of application is now more flexible because it is now instructed by the configuration class.

Summary

Hence spring dependency injection helps you in the way that, you have to declare the components and the dependencies between them and the DI container binds them together at the run-time.

--

--

Nandita Pandey
Analytics Vidhya

I’m an enthusiast learner. Here to share my learning to the people interested