Constructor Dependency Injection in Spring
Constructor Dependency Injection in Spring
(/)
Constructor Dependency
Injection in Spring
Last modi ed: September 16, 2019
by baeldung (https://www.baeldung.com/author/baeldung/)
Spring (https://www.baeldung.com/category/spring/) +
1. Introduction
https://www.baeldung.com/constructor-injection-in-spring 1/7
1/4/2020 Constructor Dependency Injection in Spring | Baeldung
1 <dependency>
2 <groupId>org.springframework</groupId>
3 <artifactId>spring-context</artifactId>
4 <version>5.1.4.RELEASE</version>
5 </dependency>
Then we need to set up a Con guration le. This le can be either a POJO or if
you prefer, an XML le.
Further reading:
Intro to Inversion of Control and Dependency Injection
with Spring (https://www.baeldung.com/inversion-
control-and-dependency-injection-in-spring)
A quick introduction to the concepts of Inversion of Control and Dependency
Injection, followed by a simple demonstration using the Spring Framework
https://www.baeldung.com/constructor-injection-in-spring 2/7
1/4/2020 Constructor Dependency Injection in Spring | Baeldung
Java con guration le looks pretty much like a plain-old Java object with
some additional annotations:
1 @Configuration
2 @ComponentScan("com.baeldung.constructordi")
3 public class Config {
4
5 @Bean
6 public Engine engine() {
7 return new Engine("v8", 5);
8 }
9
10 @Bean
11 public Transmission transmission() {
12 return new Transmission("sliding");
13 }
14 }
Here we are using annotations to notify Spring runtime that this class is a
provider of bean de nitions (@Bean annotation) and that a context scan for
additional beans needs to be performed in package com.baeldung.spring.
Next, we de ne a Car class:
1 @Component
2 public class Car {
3
4 @Autowired
5 public Car(Engine engine, Transmission transmission) {
6 this.engine = engine;
7 this.transmission = transmission;
8 }
9 }
Spring will encounter our Car class while doing a package scan and will
initialize its instance by calling the @Autowired annotated constructor.
https://www.baeldung.com/constructor-injection-in-spring 3/7
1/4/2020 Constructor Dependency Injection in Spring | Baeldung
As of Spring 4.3, classes with a single constructor can omit the @Autowired
annotation. A nice little bit of convenience and boilerplate removal!
On top of that, also starting with 4.3, constructor-based injection can be
leveraged in @Con guration annotated classes. And yes, if such a class has
only one constructor the @Autowired annotation can be omitted as well.
https://www.baeldung.com/constructor-injection-in-spring 4/7
1/4/2020 Constructor Dependency Injection in Spring | Baeldung
5. Conclusion
This quick tutorial has showcased the basics of two distinct ways to use
Constructor-Based Dependency Injection using Spring framework.
The full implementation of this tutorial can be found over on Github
(https://github.com/eugenp/tutorials/tree/master/spring-di).