Monday, 22 May 2017

What are the different types of dependency injections in spring?

Basically, instead of having your objects creating a dependency or asking a factory object to make one for them, you pass the needed dependencies into the object externally, and you make it somebody else's problem. This "someone" is either an object further up the dependency graph, or a dependency injector (framework) that builds the dependency graph.

Spring supports 2 types of dependency injection
Spring supports two types of dependency Injection, using setter method e.g. setXXX() where XXX is a dependency or via a constructor argument.

1) Constructor- based dependency injection
It is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.

2) Setter-based dependency injection
It is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

Spring Setter vs. Constructor Injection
1. Partial dependency: Partial dependency can be injected using setter injection but it is not possible by the constructor. Suppose there are 4 properties in a class, having 4 arguments constructor and setters methods. In such case, if you want to pass information to only one property, it is possible by setter method only.

2. Overriding: Setter injection overrides the constructor injection. If we use constructor and setter injection, IOC container will use the setter injection.

3. Changes: We can easily change the value by setter injection. It doesn't create a new bean instance always like a constructor. So setter injection is flexible than constructor injection.

4. Because of using setter method, setter Injection in more readable than constructor injection in Spring configuration file usually applicationContext.xml.

5. There is one drawback of setter Injection is Security. By using setter injection, we can override certain dependency which is not possible which is not possible with constructor injection because every time we call the constructor, a new object is gets created.

6. Setter and Constructor injection in Spring, where later can help if there is a circular dependency between two object A and B.

When to use Setter Injection over Constructor Injection in Spring?
Setter Injection has upper hand over Constructor Injection in terms of readability. Since for configuring Spring, we use XML files, readability is a much bigger concern. Also, the drawback of setter Injection around ensuring mandatory dependency injected or not can be handled by configuring Spring to check dependency using "dependency-check" attribute of the tag.

Once the number of dependencies crossed a threshold e.g. 5 or 6 its handy manageable to passing dependency via the constructor. Setter Injection is preferred choice when the number of dependencies to be injected is a lot more than normal, if some of those arguments are optional than using Builder design pattern is also a good option.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...