Spring Dependency
Injection
Ramesh Fadatare ( Java Guides)
Dependency Injection
1. Dependency Injection is a design pattern on which dependency
of the object is injected by the framework rather than created by
Object itself - It is also called IOC (Inversion of Control
2. Dependency Injection reduces coupling between multiple
objects as its dynamically injected by the framework.
3. Spring IoC Container uses DI to inject one object into another
object
4. There are mainly three types of Dependency Injection:
Constructor Injection, Setter Injection and Field Injection.
Ramesh Fadatare ( Java Guides)
.
Dependency Injection
1. Dependency: An object usually requires objects of
other classes to perform its operations. We call these
objects dependencies.
2. Injection: The process of providing the required
dependencies to an object.
Ramesh Fadatare ( Java Guides)
Dependency Injection Types
Dependency Injection
Constructor Base Field Base
Setter Base
Dependency Injection Dependency Injection
Dependency Injection
Ramesh Fadatare ( Java Guides)
d
Constructor Injection
• Constructor injection uses the constructor to inject dependency
on any Spring-managed bean.
• Before Spring 4.3, we had to add an @Autowired annotation
to the constructor. With newer versions, this is optional if the
class has only one constructor.
• When we have a class with multiple constructors, we need to
explicitly add the @Autowired annotation to any one of the
constructors so that Spring knows which constructor to use to
inject the dependencies.
Ramesh Fadatare ( Java Guides)
Why Should We Use Constructor Injection?
Here are advantages of using constructor injection
1. All Required Dependencies Are Available at
Initialization Tim
2. Identifying Code Smell
3. Preventing Errors in Test
4. Immutability
Ramesh Fadatare ( Java Guides)
e
Setter Injection
• Setter injection uses the setter method to inject dependency
on any Spring-managed bean.
• We have to annotate the setter method with the @Autowired
annotation.
• Spring will nd the @Autowired annotation and call the
setter to inject the dependency.
Ramesh Fadatare ( Java Guides)
fi
Field Injection
• As the name says, the dependency is injected directly in the
eld, with no constructor or setter needed. This is done by
annotating the class member with the @Autowired
annotation.
• Spring container uses re ection to inject the dependencies,
which is costlier than constructor-based or setter-based
injection.
Ramesh Fadatare ( Java Guides)
fi
fl
Field Injection Drawbacks
• You cannot create immutable objects, as you can with constructor
injection (you can’t make eld nal)
• Your classes have tight coupling with Spring IoC container and
cannot be used outside of it
• Your classes cannot be instantiated (for example in unit tests)
without re ection. You need the Spring IoC container to instantiate
them, which makes your tests more like integration tests
• Having too many dependencies is a red ag that the class usually
does more than one thing, and that it may violate the Single
Responsibility Principle.
Ramesh Fadatare ( Java Guides)
fl
fi
.
fi
.
fl
.
When to Use Constructor-based and
Setter-based DI in Spring?
• Use constructor-based DI for mandatory dependencies so
that your bean is ready to use when it is rst time called.
• Use setter-based DI only for optional dependencies
• Use setter injection to avoid circular dependencies
Ramesh Fadatare ( Java Guides)
fi
.
Which one is recommended
• Spring team recommended to use constructor based-
dependency injection.
Here are advantages of using constructor injection
1. All required dependencies are available at initialization
time (this reduces the code as well
2. Immutability and avoid NullPointerExceptio
3. Preventing errors in Tests
Ramesh Fadatare ( Java Guides)
)