Spring Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:


1. Introduction

Arguably one of the most important development principles of modern software design is Dependency Injection (DI) which quite naturally flows out of another critically important principle: Modularity.

This article will explore a specific 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.

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


Then we need to setup a Configuration file. This file can be either a POJO or, if you prefer, an xml file.

Further reading:

Intro to Inversion of Control and Dependency Injection with 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

Top Spring Framework Interview Questions

A quick discussion of common questions about the Spring Framework that might come up during a job interview.

Read more

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

This article will compare and contrast the use of annotations related to dependency injection, namely the @Resource, @Inject, and @Autowired annotations.

Read more

2. Annotation Based Configuration

Java configuration file looks pretty much like a plain-old java object with some additional annotations:

public class Config {

    public Engine engine() {
        return new Engine("v8", 5);

    public Transmission transmission() {
        return new Transmission("sliding");

Here we are using annotations to notify Spring runtime that this class is a provider of bean definitions (@Bean annotation) and that a context scan for additional beans needs to be performed in package com.baeldung.spring. Next, we define a Car class:

public class Car {

    public Car(Engine engine, Transmission transmission) {
        this.engine = engine;
        this.transmission = transmission;

Spring will encounter our Car class while doing a package scan and will initialize its instance by calling the @Autowired annotated constructor.

Instances of Engine and Transmission will be obtained by calling @Bean annotated methods of the Config class. Finally, we need to bootstrap an ApplicationContext using our POJO configuration:

ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
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 @Configuration annotated classes. And yes, if such a class has only one constructor the @Autowired annotation can be omitted as well.

4. XML Based Configuration

Another way to configure Spring runtime with constructor-based dependency injection is to use an xml configuration file:

<beans xmlns="http://www.springframework.org/schema/beans"

    <bean id="toyota" class="com.baeldung.spring.domain.Car">
        <constructor-arg index="0" ref="engine"/>
        <constructor-arg index="1" ref="transmission"/>

    <bean id="engine" class="com.baeldung.spring.domain.Engine">
        <constructor-arg index="0" value="v4"/>
        <constructor-arg index="1" value="2"/>

    <bean id="transmission" class="com.baeldung.spring.domain.Transmission">
        <constructor-arg value="sliding"/>


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 flag on.

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

ApplicationContext context = new ClassPathXmlApplicationContext("baeldung.xml");
Car car = context.getBean(Car.class);

5. Conclusion

This quick tutorial has showcased the basics of two distinct ways to use Contractor-Based Dependency Injection using Spring framework.

The full implementation of this tutorial can be found over on Github.

Spring bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2: