Spring - Setter Injection vs Constructor Injection
Last Updated :
23 Jul, 2025
Spring Framework is built on top of servlets and one of its main features is Dependency Injection (DI) it's one of the most common principles of modern software development and it allows us to achieve the principle of Inversion Of Control (IoC) i.e. shifting the creation, management and the lifecycle of objects from the programmer to the framework. This approach helps relieve the programmer from the responsibility of managing and creating objects and allows them to focus mainly on the business logic thereby effectively managing the objects. It's also one of the core features of the spring-core module and aids in promoting decoupling among the components of our system. There are various ways in which we can do this, however, in this article, we'll focus on Constructor and Setter Injections.
Pre-requisites:
The below image briefly explains the principle of IoC, that we only need to ask for what object we want, and our spring container will return an instance of it.
To summarize, the whole idea of saying - "hey, give me a smart phone object" is that if the object has any helper components or other dependencies, then assemble them ahead of time and provide me the object whenever I ask on demand or when another dependent object is created.
Types of Configuration
There are 3 types of configuration mainly used :
Step-by-Step Procedure
1. Go to Spring Initializr tool.
2. Create a package with following details - ( See image - you can modify accordingly)
3. Download the file and open it in your preferred IDE.
4. And then finally start the coding.
Constructor Injection
In this method, dependencies are passed as a parameter to the class's constructor. This ensures that all the dependencies are injected upon the instantiation of an object. We utilise the @Autowired Annotation on top of the constructors to achieve this.
Let's understand with code example:
We'll have following components in the code : Interface, concrete class that provide its implementation. And a REST Controller.
1. Interface SmartPhone
Java
package com.geeksforgeeks.ConstructorInjectionDemo;
public interface SmartPhone {
String getSmartPhoneName();
}
Code explanation:
We've simply created an interface - 'SmartPhone' having a single method inside it.
2. Concrete Class - Nokia
Java
package com.geeksforgeeks.ConstructorInjectionDemo;
import org.springframework.stereotype.Component;
@Component
public class Nokia implements SmartPhone {
@Override public String getSmartPhoneName()
{
return "This is NOKIA Smart Phone";
}
}
Code Explanation:
- This is a concrete class that implements the
SmartPhone
interface and overrides the method defined in the interface. - The
@Component
annotation indicates to the Spring framework that this is a Java bean and it should be registered for component scanning so that it can be injected into other beans when required.
3. REST Controller
Java
package com.geeksforgeeks.ConstructorInjectionDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class RESTController {
private final SmartPhone mySmartPhone;
//* Constructor Injection *//
@Autowired
public RESTController(SmartPhone theSmartPhone)
{
this.mySmartPhone = theSmartPhone;
}
@GetMapping("/smartphone/name")
public String getSmartPhoneName()
{
return mySmartPhone.getName();
}
}
Code Explanation:
- This is a rest controller containing API calls.
- The @RestController Annotation is a specialized version of @Controller and it signifies that this class is a CONTROLLER and inside it, end-points are defined and these end-points return data either in JSON or XML Format.
- SmartPhone interface Reference is here, to hold an object of concrete implementation.
- @Autowired Annotation - This is the real hero here which injects dependency. The constructor will receive the object of SmartPhone upon instantiation and the @Autowired annotation will inject it into current Rest controller's instance.
Here's how the @Autowired injects dependencies behind the scenes !
@Autowired working in layman's term
Spring will scan for @Component, and ask - Hey, is there any concrete class implementing the interface? In our case the interface is 'SmartPhone'. If there exists one (or more), then inject it.
And finally, our @SpringBootApplication class.
4. Application class:
Java
package com.geeksforgeeks.ConstructorInjectionDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConstructorInjectionDemoApplication {
public static void main(String[] args)
{
SpringApplication.run(ConstructorInjectionDemoApplication.class,args);
}
}
After creating all the classes and interfaces, our package structure will look something like this :
And when we run our application, the output will be :

A key point to note here is that, if we have more than 1 concrete implementation for our interface then our application won't RUN!
In the above case, we can utilize both of these Annotations - @Qualifier or @Primary.
Setter Injections
This is another way to inject dependencies, done by using setter methods. We annotate a setter method with @Autowired
and it will work. A key point here is that dependencies will only be injected when the setter methods are called.
Assume that all the code will remain the same as we previously wrote for constructor injections, except for a new concrete implementation Vivo
and the REST Controller. There are only subtle modifications required in the REST Controller:
Java
package com.geeksforgeeks.SetterInjectionDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class RESTController {
private SmartPhone mySmartPhone;
//* Setter Injection *//
@Autowired
public void setSmartPhone(SmartPhone theSmartPhone) {
this.mySmartPhone = theSmartPhone;
}
@GetMapping("/smartphone/name")
public String getSmartPhoneName() {
return mySmartPhone.getSmartPhoneName();
}
}
Code Explanation:
- Instead of having a constructor, here we have a setter method that injects the dependency because its annotated with @Autowired.
- Breaking down how @Autowired is working here:
Code Explanation:
- Firstly, the target class object is created
- Secondly, the dependent class object is created
- And finally, the dependencies are injected through setter method.
Output:
Note: Its not necessary that we can only use setter method to inject dependencies instead, we can annotate any method with @Autowired and it'll work as a dependency injection
3. Any method as the dependency injection
Any method name will work as long as the method that's injecting dependencies is annotated with @Autowired
Java
//* Any method name will work *//
@Autowired
public void doSomething(SmartPhone theSmartPhone) {
this.mySmartPhone = theSmartPhone;
}
Now you maybe wondering which one should you choose while injecting dependencies? So, here we've discussed below some parameter based differences between them:
Setter Injections vs Constructor Injections
Parameter | Setter Injections | Constructor Injections |
---|
Type of dependency | Recommended to use when we have some optional dependency. Else our application can provided default business logic code. | Recommended to use when Mandatory/required dependencies are there |
---|
Injection method | Through methods | Through Constructors |
---|
Creation time | IoC Container first creates the dependent object then the Target Class Object | IoC Container first create the target class object then the dependent object |
---|
Efficiency | Slow - because dependencies are injected after object of target class is created | Fast - because Dependencies are injected as soon as the object of target class is created |
---|
Recommendation | Not recommended as the first choice | Recommended by spring.io team as your first choice |
---|
Use cases when to use which one?
1. Constructor Injections
They are most useful when we need an object along with all its dependencies. For example, a CAR can have various dependencies like engine, gear, steering, and other relevant components without which a CAR is useless. So, we need all those components wired together in the object of a car.
2. Setter Injections
They are preferred when we have some optional dependencies that are not mandatorily required but can assist in some ways. For example, let’s say we have a new user in our application, and they have their phone number as an optional field in their profile details.
Conclusion
In this article, we have learned about Constructor and Setter injections, how to use them, and the pros and cons of each. Dependency injection is a powerful feature of Spring, and choosing the right type of injection can make your application more maintainable and efficient.
Similar Reads
Spring - Injecting Literal Values By Constructor Injection Spring IoC (Inversion of Control) Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application. It gets the informatio
5 min read
Spring - Injecting Objects By Constructor Injection Spring IoC (Inversion of Control) Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, and manages their entire life cycle. The Container uses Dependency Injection (DI) to manage the components that make up the application. It gets the infor
5 min read
Spring - Setter Injection with Collection Dependency Injection is the main functionality provided by Spring IOC(Inversion of Control). The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods. In Setter Dependency Injection(SDI) the dependency will be injected with the help of setters and
2 min read
Spring Dependency Injection: @Autowired vs Constructor Injection In Spring framework, dependency injection is a key idea that lets you inject dependencies into a class in preference to hard coding them. This provides loose coupling and makes the code extra maintainable and testable. There are distinct methods to perform dependency injection.Common Approaches to
4 min read
Spring - Injecting Literal Values By Setter Injection Spring IoC (Inversion of Control) Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application. It gets the informatio
4 min read
Spring - Constructor Injection with Dependent Object In the constructor injection, the dependency injection will be injected with the help of constructors. Now to set the dependency injection as constructor dependency injection(CDI) in bean, it is done through the bean-configuration file For this, the property to be set with the constructor dependency
3 min read