0% found this document useful (0 votes)
122 views

Spring Dependency Injection Class Notes

This document discusses dependency injection in Spring. It explains that dependency injection involves a framework injecting dependencies into objects rather than having objects create dependencies. There are three main types of dependency injection in Spring: constructor injection, setter injection, and field injection. Constructor injection uses a class constructor to inject dependencies, while setter injection uses setter methods. Field injection directly injects dependencies into fields. Constructor injection is generally recommended over other options.

Uploaded by

George Popa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Spring Dependency Injection Class Notes

This document discusses dependency injection in Spring. It explains that dependency injection involves a framework injecting dependencies into objects rather than having objects create dependencies. There are three main types of dependency injection in Spring: constructor injection, setter injection, and field injection. Constructor injection uses a class constructor to inject dependencies, while setter injection uses setter methods. Field injection directly injects dependencies into fields. Constructor injection is generally recommended over other options.

Uploaded by

George Popa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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)


)

You might also like