What is the use of @Order annotation in Spring?

It is used for Advice execution precedence.

The highest precedence advice runs first. The lower the number, the higher the precedence. For example, given two pieces of ‘before’ advice, the one with highest precedence will run first.

Another way of using it is for ordering Autowired collections

@Component
@Order(2)
class Toyota extends Car {
    public String getName() {
        return "Toyota";
    }
}

@Component
@Order(1)
class Mazda extends Car {
    public String getName() {
        return "Mazda";
    }
}

@Component
public class Cars {
    @Autowired
    List<Car> cars;

    public void printNames(String [] args) {

        for(Car car : cars) {
            System.out.println(car.getName())
        }
    }
}

You can find executable code here: https://github.com/patrikbego/spring-order-demo.git

Hope this clarifies it a little bit further.

Output:-

Mazda Toyota

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)