Spring @Order annotation

In this post under Spring Core, I will explain with example the purpose of “@Order” annotation.

In one of my previous posts under Spring Core, I have explained how to autowire collections, array, and map.

For the purpose of recap, lets assume we have a class “Dao” which we want to inject in class “Example22” as shown in the below code snippet

package package22;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class Example22 {
    @Autowired
    private List<Dao> daoList;

    @Autowired
    private Dao[] daoArray;

    @Bean
    protected Dao jmsDao() {
        return new Dao("JMSDao");
    }

    @Bean
    protected Dao dbDao() {
        return new Dao("DBDao");
    }

    public List<Dao> getDaoList() {
        return daoList;
    }

    public Dao[] getDaoArray() {
        return this.daoArray;
    }

    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example22.class);
        Example22 example = applicationContext.getBean(Example22.class);
        System.out.println("Browsing the list");
        for(Dao dao : example.getDaoList()) {
            System.out.println(dao);
        }
        System.out.println("Browsing the array");
        for(Dao dao : example.getDaoArray()) {
            System.out.println(dao);
        }
    }
}

When the above code is executed, the Spring framework will populate “daoList” and “daoArray”. The elements in the collection and array will be in the below order
1) JMSDao instance
2) DBDao instance

The Spring framework call bean definition methods (i.e., methods annotated with “@Bean”) in the order they are declared and populate the collection and array in that order.

So in this case, since “jmsDao” bean definition method is declared first, an instance of it is created and populated in the collection and in the array at first position.

Then since “dbDao” bean definition method is declared second, an instance of it is created and populated in the collection and in the array at second position.

We can change this order by applying “@Order” annotation at the bean definition methods as shown below

1  package package25;
2  
3  import org.springframework.beans.factory.annotation.Autowired;
4  import org.springframework.context.ApplicationContext;
5  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6  import org.springframework.context.annotation.Bean;
7  import org.springframework.context.annotation.Configuration;
8  import org.springframework.core.annotation.Order;
9  
10 import java.util.List;
11 
12 @Configuration
13 public class Example25 {
14     @Autowired
15     private List<Dao> daoList;
16 
17     @Bean
18     @Order(2)
19     protected Dao jmsDao() {
20         return new Dao("jmsDao");
21     }
22 
23     @Bean
24     @Order(1)
25     protected Dao dbDao() {
26         return new Dao("dbDao");
27     }
28 
29     public List<Dao> getDaoList() {
30         return daoList;
31     }
32 
33     public static void main(String[] args) {
34         ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example25.class);
35         Example25 example = applicationContext.getBean(Example25.class);
36         for(Dao dao : example.getDaoList()) {
37             System.out.println(dao);
38         }
39     }
40 }

In the above code, “jmsDao” method is annotated with “@Order” annotation and value being “2”. So it is telling Spring framework when creating a collection or map or array, place this bean at second position.

In the above code, “dbDao” method is annotated with “@Order” annotation and value being “1”. So it is telling Spring framework when creatinga a collection or map or array, place this bean at first position.

In this way we can use “@Order” annotation.

Leave a Reply