Depends-on in Spring

Last Updated on November 16th, 2020 at 07:44 pm

With this example I will show you how depends-on (@DependsOn annoattion) works in Spring application. Depends-on is basically used when you have a dependency on another bean without which you cannot proceed with the execution of your operations in current bean. Or if you need to initialize any bean before another bean, depends-on does this job. While creating bean you need to define depends-on attribute in bean. Here I will also show you how a bean depends on other multiple beans.

When a bean is typically dependent on another bean then we specify the dependent bean using @DependsOn annotation in Java based configuration metadata. However, sometimes dependencies between beans are less direct; for example, a static initializer that initializes something like loading of database driver in a class needs to be triggered first. The depends-on attribute can explicitly force one or more beans to be initialized before the bean using this element is initialized.


Java at least 8, Spring Boot 2.4.0, Gradle 6.5.1 or Maven 3.6.3

Project Setup

You create either maven or gradle based project in your favorite tool or IDE. The name of the project is spring-depends-on-example.

For gradle based project you can use the following build.gradle script:

buildscript {
	ext {
		springBootVersion = '2.4.0'
    repositories {
    	maven {
    		url ''
    dependencies {

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {

For maven based project you can use the below pom.xml file:

<project xmlns=""





					<source>at least 8</source>
					<target>at least 8</target>

Spring Beans

Here I am going to create some beans and going to show you how to use depends-on using @DepensOn annotation. This annotation has to be placed on class level. You can pass multiple bean names if your bean has dependency on multiple beans. Bean name is determined from the class name with initial letter in lower case.


This bean is dependent on other beans.

package com.roytuts.spring.depends.on.example;

import org.springframework.stereotype.Component;

public class BeanB {

    private String name;

    public BeanB() {
        System.out.println("BeanB is initialized.");

    public String getName() {
        return name;

    public void setName(String name) { = name;



This bean depends on BeanB and will throw error if BeanB is not found. If your bean depends on a single bean then you don’t need to specify the bean name inside {} and can be specified simply @DependsOn(value = "beanB" ) or @DependsOn("beanB").

package com.roytuts.spring.depends.on.example;

import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

@DependsOn(value = { "beanB" })
public class BeanC {

    public BeanC() {
        System.out.println("BeanC is initialized.");



This bean depends on multiple beans BeanB and BeanC.

package com.roytuts.spring.depends.on.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

@DependsOn(value = { "beanB", "beanC" })
public class BeanA {

    private BeanB beanB;

    public BeanA() {
        System.out.println("Initializing BeanA");

    public BeanB getBeanB() {
        return beanB;

    public void setBeanB(BeanB beanB) {
        this.beanB = beanB;


Main Class

In Spring boot application you just need @SpringBootApplication and a main method in a class to start your application.

package com.roytuts.spring.depends.on.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDependsOnApp implements CommandLineRunner {

    private BeanA beanA;

    public static void main(String[] args) {, args);

    public void run(String... args) throws Exception {



Testing the Application

Running the above main class, you will get below output:

BeanB is initialized.
BeanC is initialized.
Initializing BeanA

Source Code


Leave a Reply

Your email address will not be published. Required fields are marked *