Close

Spring - Controlling Beans Loading Order by using @DependsOn

[Last Updated: Mar 17, 2021]

The order in which Spring container loads beans cannot be predicted. There's no specific ordering logic specification given by Spring framework. But Spring guarantees if a bean A has dependency of B (e.g. bean A has an instance variable @Autowired B b;) then B will be initialized first. But what if bean A doesn't have direct dependency of B and we still want B to initialize first?



When we want to control beans initializing order

There might be scenarios where A is depending on B indirectly. For example assume A is some kind of events publisher and B is listening to those events. This is a typical scenario of observer pattern. We don't want B to miss any events and would like to have B being initialize before A.

@DependsOn annotation

Spring provides @DependsOn indicating which bean should be initialized before this bean:



Example

Following example shows a very basic observer pattern.

EventManager, a facility to register listeners and publishing events

package com.logicbig.example.bean;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class EventManager {
  private final List<Consumer<String>> listeners = new ArrayList<>();

  @PostConstruct
  public void initialize() {
      System.out.println("initializing: "+this.getClass().getSimpleName());
  }

  public void publish(final String message) {
      listeners.forEach(l -> l.accept(message));
  }

  public void addListener(Consumer<String> eventConsumer) {
      listeners.add(eventConsumer);
  }
}

EventPublisher

package com.logicbig.example.bean;

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

public class EventPublisher {
  @Autowired
  private EventManager eventManager;

  @PostConstruct
  public void initialize() {
      System.out.println("initializing: "+this.getClass().getSimpleName());
      eventManager.publish("event published from EventPublisherBean");
  }
}

EventListener

package com.logicbig.example.bean;

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

public class EventListener {
  @Autowired
  private EventManager eventManager;

  @PostConstruct
  private void initialize() {
      System.out.println("initializing: "+this.getClass().getSimpleName());
      eventManager.addListener(s ->
              System.out.println("event received in EventListenerBean : " + s));

  }
}

Defining beans and running main class

package com.logicbig.example;

import com.logicbig.example.bean.EventListener;
import com.logicbig.example.bean.EventManager;
import com.logicbig.example.bean.EventPublisher;
import org.springframework.context.annotation.*;

@Configuration
@ComponentScan("com.logicbig.example")
public class AppConfig {

  @Bean
  @DependsOn("eventListenerBean")
  public EventPublisher eventPublisherBean() {
      return new EventPublisher();
  }

  @Bean
  public EventListener eventListenerBean() {
      return new EventListener();
  }

  @Bean
  public EventManager eventManagerBean() {
      return new EventManager();
  }

  public static void main(String... strings) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      context.close();

  }
}

Output

initializing: EventManager
initializing: EventListener
initializing: EventPublisher
event received in EventListenerBean : event published from EventPublisherBean

If we don't use @DependsOn, there's no guarantee that EventListener will initialize first:

@Configuration
@ComponentScan("com.logicbig.example")
public class AppConfig {

    @Bean
   // @DependsOn("eventListenerBean")
    public EventPublisher eventPublisherBean() {
        return new EventPublisher();
    }

    @Bean
    public EventListener eventListenerBean() {
        return new EventListener();
    }

    @Bean
    public EventManager eventManagerBean() {
        return new EventManager();
    }

    public static void main(String... strings) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        context.close();

    }
}

Output

initializing: EventManager
initializing: EventPublisher
initializing: EventListener

Dependencies and Technologies Used:

  • Spring Context 4.2.3.RELEASE: Spring Context.
  • JDK 1.8
  • Maven 3.0.4

@DependsOn Example Select All Download
  • spring-depends-on
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • bean
                • AppConfig.java

    See Also