Spring gives the developer the control of how beans are wired. However, Spring also empowers developers with greater control over how beans are wired. There are a variety of options to choose from. In this article will focus on Constructor injection and Setter injection.
The code for this example can be found in github, CoffeShop. This example is under the package com.programmingsquirrel.coffeeshop.injection.
Let’s begin by creating another coffee-making implementation called MakeCoffeAnotherImplementation. Now he have two implementations MakeCoffeImplementation and MakeCoffe. AnotherImplementation
MakeCoffeImplementation will utilize Constructor Injection, while MakeCoffe will employ the Setter Injection. To achieve these injections, we will use the AnotherImplementation@Autowired annotation at different parts of the code to instruct Spring on which injection type to use.

Constructor Injection
To use this type of injection, we will create a constructor in our MakeCoffeImplementation class, in this constructor we will assign the coffee bean to the coffee implementation.
@Component
public class MakeCoffeeImplementation {
private Coffee coffee;
@Autowired
MakeCoffeeImplementation(@Qualifier("Latte") Coffee coffee) {
this.coffee = coffee;
System.out.println("Make Coffee Implementation Constructor invoked");
}
//...
}
In the MakeCoffeImplementation we add a constructor that receives Coffee, in the method declaration we add the @Autowire annotation. Then we add the @Qualifier annotation with the name of the bean the will be injected into the dependency, in this case the bean to be injected is “Latte” (LatteCoffee). When the application is run, Spring injects the LatteCoffee bean in the MakeCoffeeImplementation class using the constructor. The next message is printed on the console.
Make Coffee Implementation Constructor invoked
2023-07-10 15:43:59.825 INFO 3601 --- [ main] c.p.c.injection.CoffeeshopApplication : Started CoffeeshopApplication in 0.776 seconds (JVM running for 1.113)
Making a Latte...
The Coffee is ready
If we don’t specify the bean with the @Qualifier annotation, Spring will be use the bean with the @Primary annotation, in this example if we use the next form of the constructor we will use the MochaCoffee bean.
@Component
public class MakeCoffeeImplementation {
private Coffee coffee;
@Autowired
MakeCoffeeImplementation(Coffee coffee) {
this.coffee = coffee;
System.out.println("Make Coffee Implementation Constructor invoked");
}
//...
}

Setter Injection
The other way to handle the wiring of the dependency is using the setter method. In the MakeCoffeAnotherImplementation we will create the setter method for the field coffee.
@Component
public class MakeCoffeeAnotherImplementation {
private Coffee coffee;
@Autowired
@Qualifier("Mocha")
public void setCoffe(Coffee coffee){
this.coffee = coffee;
System.out.println("Make Coffee Setter Implementation invoked");
}
//...
}
In the MakeCoffeImplementation, we have added a setter method for the coffee field, Also we included the @Autowired annotation along with the @Qualifier annotation, specifying the name of the bean that will be injected, which in this case “Mocha” (MochaCoffee).
When the application is executed, Spring injects the MochaCoffee bean into the MakeCoffeeAnotherImplementation class using the setter method setCoffee(). The message "Make Coffee Setter Implementation invoked" is printed on the console, indicating that Spring uses the setter injection.
Make Coffee Setter Implementation invoked
Make Coffee Implementation Constructor invoked
2023-07-10 18:25:13.377 INFO 4217 --- [ main] c.p.c.injection.CoffeeshopApplication : Started CoffeeshopApplication in 0.918 seconds (JVM running for 1.263)
Making a Latte...
The Coffee is ready
Making a Mocha...
The Coffee is ready

Field Injection
We have covered two ways that Spring handles dependency injection so far. However, if you have read the previous entries: Autowiring by type – @Primary, Autowiring by Name, and Autowiring – @Qualifier, you might notice that none of these approaches were used on these entries. This is because we have a third way to indicate Spring to perform the dependency injection. The Field Injection.
In this type of injection, we eliminate the need for a constructor or a setter method, and instead utilize a field of type Coffee called coffee. Using Field Injection keeps the code simple and readable. However, it comes with certain risks, as Spring has the ability to set private fields of the objects, makin it potentially unsafe. Testing can also becomes inconvenient because we require a way to perform dependency injection specially for testing purposes. Another drawback is that a developers may introduce a lot of optional dependencies, which can lead to increased complexity in the application. Alternatively, if a constructor were used, each additional dependency would result in an increase on the number of constructor arguments.

Which injection one is best?
We already saw that Field Injection keeps the code simple and readable, but it has a lot of problems when we test the code and the application becomes complex when we add more dependencies to the application. So which one of the other two we need to use if the use of the Constructor or Setter injection result in the same outcome.
Constructor injection ensures that all dependencies are injected because an object cannot be constructed until all its dependencies are available. It also ensures immutability as the state of the bean cannot be modified after creation, but in some cases the BeanCurrentlyInCreationException is rised.
The Setter Injection is used to avoid the BeanCurrentlyInCreationException. But also setter injection injects the dependencies when are needed, not when the bean is created.
The decision of which one to use depends on your scenario, Springs recommends the Constructor Injection, but if you have a larger number of optional dependencies Setter Injection can allow you to set them selectively, potentially reducing the memory usage by avoiding unnecessary object creation.
Thanks for reading and happy Learning!!!


Leave a comment