Spring framework: is most widely used free and open source java application framework. It was created by Rod Johnson in the year 2003 and is very popular among the developers around the globe.
The main aim of developing this framework was to enhance the developer’s productivity and also make them understand and use some of the best industry coding practices.
The Spring framework was created by using a design pattern Inversion of control (IOP) or Dependency Injection,
Let us understand this with the help of an example.
Here we are creating object of Class B inside Class A which means class A is completely dependent upon class B. In future if there will be any modification in Class B (number of Parameters) we may be forced to do modification in Class A as well.
Again, If we want to replace Class B with some other Class (say Class C) which provides more better functionality then in that case also we will have to modify Class A.
This is a very small example. In big projects where there are hundreds of java classes doing such changes can be a big pain. These problems are just for one reason that Class A is completely dependent on class B
So there needs to be a mechanism by which we can remove this dependency or reduce this to overcome all such problems.
Here comes Spring Inversion of Control(IOP) or Dependency Injection Container in picture.
Inversion of control (IOP) or Dependency Injection – It says that you should not be creating or instantiating an object inside any other java class. Instead , rely on Spring’s IOP module to crate the object for you.
Understanding the problem if we don’t use Dependency Injection
In this example we have a Hotel class that needs Coffee class to prepare Drink. Calling prepareDrink() method of Hotel class will prepareCoffee using the coffee object. But this can cause problems in future maintenance of the project as we are directly instantiating coffee object inside Hotel Class using a new operator and because of this the Hotel class has a direct dependency on the Coffee Class or so much tightly coupled with Coffee class that it will cause so much trouble if in future we decide to change the Coffee Class.
Problems that will be faced if we use this design
- If we will replace Coffee Class with some other class( ColdCofee) then this will force us to do modification in Hotel class as well.
- Modifying the Coffee class (parameterising its constructor) will also need to do changes in Hotel class.
- You cannot test Hotel Class without including Coffee Class.
If Hotel class just uses the Coffee object without mentioning Coffee class in it then we can overcome all such problems.
The framework that helps you in making this possible is the Dependency Injection Framework. You will not have to mention Coffee object inside Hotel class and still the Hotel class will use the Coffee object.
It says never create an object inside another Java class using the new operator, instead the framework will create all the objects along with their dependencies in your application and you just need to use them. This will make the code more readable and maintainable.
In the next blog we will see how we can use Dependency Injection and the ways by which we can use this >>>