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

Constructor Dependency Injection in Spring

This document discusses constructor dependency injection in Spring. It describes how to configure constructor injection using annotations or XML. With annotations, you can define beans and inject dependencies into a class's constructor using @Autowired. With XML, you define beans and inject constructor arguments using <constructor-arg>. The document provides examples of defining beans and injecting dependencies into a Car class's constructor using both annotations and XML configuration.

Uploaded by

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

Constructor Dependency Injection in Spring

This document discusses constructor dependency injection in Spring. It describes how to configure constructor injection using annotations or XML. With annotations, you can define beans and inject dependencies into a class's constructor using @Autowired. With XML, you define beans and inject constructor arguments using <constructor-arg>. The document provides examples of defining beans and injecting dependencies into a Car class's constructor using both annotations and XML configuration.

Uploaded by

Shubham Mittal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/4/2020 Constructor Dependency Injection in Spring | Baeldung

(/)

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/) +

Spring Core Basics (https://www.baeldung.com/tag/spring-core-basics/)


Spring DI (https://www.baeldung.com/tag/spring-di/)

I just announced the new Learn Spring course, focused on


the fundamentals of Spring 5 and Spring Boot 2:
>> CHECK OUT THE COURSE (/ls-course-start)

1. Introduction

Arguably one of the most important development principles of modern


software design is Dependency Injection (DI) which quite naturally ows out of
another critically important principle: Modularity.
This article will explore a speci c type of DI technique called Constructor-
Based Dependency Injection within Spring – which simply put, means that
required components are passed into a class at the time of instantiation.

https://www.baeldung.com/constructor-injection-in-spring 1/7
1/4/2020 Constructor Dependency Injection in Spring | Baeldung

To get started we need to import spring-context dependency in our pom.xml:

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

Read more (https://www.baeldung.com/inversion-control-and-


dependency-injection-in-spring) →

Top Spring Framework Interview Questions


(https://www.baeldung.com/spring-interview-
questions)
A quick discussion of common questions about the Spring Framework that
might come up during a job interview.

Read more (https://www.baeldung.com/spring-interview-questions) →

Wiring in Spring: @Autowired, @Resource and @Inject


(https://www.baeldung.com/spring-annotations-
resource-inject-autowire)
This article will compare and contrast the use of annotations related to
dependency injection, namely the @Resource, @Inject, and @Autowired
annotations.

https://www.baeldung.com/constructor-injection-in-spring 2/7
1/4/2020 Constructor Dependency Injection in Spring | Baeldung

Read more (https://www.baeldung.com/spring-annotations-resource-


inject-autowire) →

2. Annotation Based Con guration

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

Instances of Engine and Transmission will be obtained by calling @Bean


annotated methods of the Con g class. Finally, we need to bootstrap an
ApplicationContext using our POJO con guration:

1 ApplicationContext context = new AnnotationConfigApplicationContext(Conf


2 Car car = context.getBean(Car.class);

3. Implicit Constructor Injection

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.

4. XML Based Con guration

Another way to con gure Spring runtime with constructor-based dependency


injection is to use an xml con guration le:

1 <bean id="toyota" class="com.baeldung.constructordi.domain.Car">


2     <constructor-arg index="0" ref="engine"/>
3     <constructor-arg index="1" ref="transmission"/>
4 </bean>
5  
6 <bean id="engine" class="com.baeldung.constructordi.domain.Engine">
7     <constructor-arg index="0" value="v4"/>
8     <constructor-arg index="1" value="2"/>
9 </bean>
10  
11 <bean id="transmission" class="com.baeldung.constructordi.domain.Transm
12     <constructor-arg value="sliding"/>
13 </bean>

https://www.baeldung.com/constructor-injection-in-spring 4/7
1/4/2020 Constructor Dependency Injection in Spring | Baeldung

Note that constructor-arg can accept a literal value or a reference to another


bean and that an optional explicit index and type can be provided. Type and
index attributes can be used to resolve ambiguity (for example if a constructor
takes multiple arguments of the same type).

name attribute could also be used for xml to java variable


matching, but then your code must be compiled with
debug ag on.

A Spring application context, in this case, needs to be bootstrapped using


ClassPathXmlApplicationContext:

1 ApplicationContext context = new ClassPathXmlApplicationContext("baeldun


2 Car car = context.getBean(Car.class);

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).

I just announced the new Learn Spring course,


focused on the fundamentals of Spring 5 and Spring
Boot 2:

>> CHECK OUT THE COURSE (/ls-course-end)

Comments are closed on this article!


https://www.baeldung.com/constructor-injection-in-spring 5/7

You might also like