Spring - @DependsOn annotation example

Posted on January 7, 2017


Technologies used: JDK 1.8.0_71 | Spring 4.3.5.RELEASE | Maven 3.3.9 | Eclipse Mars.2 (4.5.2)

The @DependsOn annotation can force Spring IoC container to initialize one or more beans before the bean which is annotated by @DependsOn annotation.

The @DependsOn annotation may be used on any class directly or indirectly annotated with @Component or on methods annotated with @Bean.

The following example shows how to use @DependsOn annotation in spring application.

@DependsOn annotation example

Consider the following beans BeanOne, BeanTwo and BeanThree.

BeanOne.java

package com.boraji.tutorial.spring;

import org.springframework.beans.factory.annotation.Autowired;

public class BeanOne {
   @Autowired
   private BeanTwo beanTwo;

   @Autowired
   private BeanThree beanThree;

   public BeanOne() {
      System.out.println("BeanOne Initialized");
   }

   public void doSomthing() {
      System.out.println("Inside doSomthing() method of BeanOne");
      beanTwo.doSomthing();
      beanThree.doSomthing();
   }
}

BeanTwo.java

package com.boraji.tutorial.spring;

public class BeanTwo {

   public BeanTwo() {
      System.out.println("BeanTwo Initialized");
   }

   public void doSomthing() {
      System.out.println("Inside doSomthing() method of BeanTwo");
   }

}

BeanThree.java

package com.boraji.tutorial.spring;

public class BeanThree {

   public BeanThree() {
      System.out.println("BeanThree Initialized");
   }

   public void doSomthing() {
      System.out.println("Inside doSomthing() method of BeanThree");
   }
}

Declare the above beans in java based configuration class.

AppConfig.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
public class AppConfig {

   @Bean("beanOne")
   @DependsOn(value = { "beanTwo", "beanThree" })
   public BeanOne getBeanOne() {
      return new BeanOne();
   }

   @Bean("beanTwo")
   public BeanTwo getBeanTwo() {
      return new BeanTwo();
   }

   @Bean("beanThree")
   public BeanThree getBeanThree() {
      return new BeanThree();
   }
}

 

Create main class and run application.

MainApp.java

package com.boraji.tutorial.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      BeanOne bean=context.getBean(BeanOne.class);
      bean.doSomthing();
      context.close();
   }
}

Output

BeanTwo Initialized
BeanThree Initialized
BeanOne Initialized
Inside doSomthing() method of BeanOne
Inside doSomthing() method of BeanTwo
Inside doSomthing() method of BeanThree

As you can see in the above output, the beans beanTwo and beanThree are initialized before bean beanOne.

If you remove the @DependsOn annotation from getBeanOne() method of AppConfig class, the output (i.e. order of initialization of beans) of main class will be different on each run.

Download Sources