Spring beans are not always loaded in the same order. The Spring container has freedom in the way beans are loaded and even in the smallest projects the order changes. One of the restrictions is a direct dependency between beans via the @Autowired annotation. In the following example Spring ensures that the ProductService is initialised before the ProductController is initialised.

public class ProductController {
    private ProductService productService;

Controlling the loading order

But what if there is no direct dependency? Suppose there is an event stream outside of the application. You have a publisher bean that pushes an event to the stream and a subscriber bean that reads the events from the stream. The issue is that you want all the events from the publisher to be read by the subscriber, so the subscriber bean must be initialised first. This can be done with the @DependsOn annotation.

public class BeanLoadingConfig {
    public PublisherBean publisherBean() {
        return new PublisherBean();
    @Bean(name = "subscriber")
    public SubscriberBean subscriberBean() {
        return new SubscriberBean();

This matches the description above in that the PublisherBean depends on the SubscriberBean so the SubscriberBean is loaded first. For the complete example see my project on github. It should output the following to prove our configuration works:

> Subsciber bean loaded
> Publisher bean loaded

If we would remove the @DependsOn annotation we might get the same result. But running the application multiple times shows that this is not always the case. The Spring container decides the order and sometimes this is the order we intended.