Dependency Injection in Spring
Dependency injection involves providing an object its dependencies rather than the object having to acquire those dependencies on its own.
The dependencies are injected into the objects that need them.
If a class X requires a class Y’s functionality, we don’t have to write number of lines of code to create an instance Y to use its methods. Spring framework injects instance of Y to object X at run time.
Dependency injection is also known as Inversion of Control or IoC. It refers to the control of creating instances being with the Spring container.
The container creates the objects and injects them into the application.
Advantages of Dependency Injection
- Lookup logic is completely removed from application code and dependencies can be injected into target components in a pluggable manner. This makes unit testing of components easy as there is no environmental dependency like JNDI context and dependent components can be easily mocked and wired up to the component in a test case.
- Configuration of application in different environments becomes easy and can be done without any code modifications as there are no concrete class dependencies within components.
There is no dependence on the container API. Code can be moved from one container to another, and it should still work without any modification in the codebase.
Types of Dependency Injection
Two Dependency Injection methods can be used. They are Constructor injection and Setter injection.
Setter injection
The setter methods are invoked immediately after the object is instantiated by the container. The injection occurs during the component creation or initialization phase, which is performed much earlier in the process than handling business method calls.
Advantage of Setter injection
It allows re‐configurability of the component after its creation. The component’s dependencies can be changed at run time.
Disadvantage of setter injection
With setter injection, all necessary dependencies may be injected before use, which leaves the component in a partially configured state.
In some cases, the order of invocation of setter methods might be important, and this is not expressed in the component’s contract.
Constructor injection
With constructor injection, beans express their dependencies via constructor arguments. In this method, dependencies are injected during component creation.
Advantage of Constructor injection
- Since the dependencies are provided during construction, the components will be in consistent state and ready to use.
- The amount of code written in constructor injection will be slightly less compared to the amount of code written if setter injection is used.
Disadvantage of Constructor injection
- The biggest disadvantage of constructor injection is that it won’t be possible to reconfigure components after creation if they don’t provide setters for the properties.
- Using several overloaded constructors for different configurations options might be confusing and also unavailable most of the time.
- Constructor injection cannot handle circular dependencies. For example, if you have two beans, and they both have dependencies to each other through their constructors, the Spring Container will not be able to instantiate those two beans.
Whether to use Setter or Constructor injection ?
Both methods have advantages as well as disadvantages, and it is not always possible to use only one method for any application.
Setter injections don’t guarantee that all necessary dependencies are provided for the component. So, you can have components created with incomplete dependency.
The constructor injection ensures all mandatory properties have been satisfied, and it is simply not possible to instantiate an object in an invalid state. Constructor injection does not allow you to create the component until all the mentioned dependencies are met.
If the components need to be reconfigurable at run time, having setters for their specific properties will be mandatory.
So, if you have third party classes that don’t have suitable constructors for your configuration, then you have to first create a component with an available constructor that accepts arguments close to your needs, and then inject other dependencies with setter methods.
So, it is advisable to use constructor injection for all mandatory collaborators and setter injection for all other properties.
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post