@Order in Spring – @Spring里的订单

最后修改: 2018年 5月 4日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’re going to learn about Spring’s @Order annotation. The @Order annotation defines the sorting order of an annotated component or bean.

在本教程中,我们将学习Spring的@Order注解。@Order注解定义了被注解的组件或bean的排序顺序。

It has an optional value argument which determines the order of the component; the default value is Ordered.LOWEST_PRECEDENCE. This marks that the component has the lowest priority among all other ordered components.

它有一个可选的值参数,决定了组件的顺序;默认值是Ordered.LOWEST_PRECEDENCE。这标志着该组件在所有其他有序组件中具有最低的优先级。

Similarly, the value Ordered.HIGHEST_PRECEDENCE can be used for overriding the highest priority among components.

同样,Ordered.HIGHEST_PRECEDENCE值可以用来覆盖组件间的最高优先级。

2. When to Use @Order

2.何时使用@Order

Before Spring 4.0, the @Order annotation was used only for the AspectJ execution order. It means the highest order advice will run first.

在Spring 4.0之前,@Order注解只用于AspectJ的执行顺序。它意味着最高顺序的建议将首先运行。

Since Spring 4.0, it supports the ordering of injected components to a collection. As a result, Spring will inject the auto-wired beans of the same type based on their order value.

从Spring 4.0开始,它支持对注入的组件进行排序,使之成为一个集合。因此,Spring将根据其顺序值注入相同类型的自动连接的Bean。

Let’s explore it with a quick example.

让我们用一个简单的例子来探讨一下。

3. How to Use @Order

3.如何使用@Order

First of all, let’s set up our project with the relevant interface and classes.

首先,让我们用相关的接口和类来设置我们的项目。

3.1. Interface Creation

3.1.界面创建

Let’s create the Rating interface that determines the rating of a product:

让我们来创建决定产品评级的Rating接口。

public interface Rating {
    int getRating();
}

3.2. Components Creation

3.2.组件创建

Finally, let’s create three components that define the ratings of some products:

最后,让我们创建三个组件,定义一些产品的评级。

@Component
@Order(1)
public class Excellent implements Rating {

    @Override
    public int getRating() {
        return 1;
    }
}

@Component
@Order(2)
public class Good implements Rating {

    @Override
    public int getRating() {
        return 2;
    }
}

@Component
@Order(Ordered.LOWEST_PRECEDENCE)
public class Average implements Rating {

    @Override
    public int getRating() {
        return 3;
    }
}

Note that the Average class has the lowest priority because of its overridden value.

注意,Average类的优先级最低,因为它的值被覆盖了。

4. Testing Our Example

4.测试我们的例子

Up until now, we’ve created all the required components and the interface to test the @Order annotation. Now, let’s test it to confirm that it works as expected:

到目前为止,我们已经创建了所有需要的组件和接口来测试@Order注解。现在,让我们测试一下,以确认它是否如预期那样工作。

public class RatingRetrieverUnitTest { 
    
    @Autowired
    private List<Rating> ratings;
    
    @Test
    public void givenOrder_whenInjected_thenByOrderValue() {
        assertThat(ratings.get(0).getRating(), is(equalTo(1)));
        assertThat(ratings.get(1).getRating(), is(equalTo(2)));
        assertThat(ratings.get(2).getRating(), is(equalTo(3)));
    }
}

5. Conclusion

5.结论

We’ve learned about the @Order annotation in this quick article. We can find the application of @Order in various use cases – where the ordering of the auto-wired components matter. One example is the Spring’s request filters.

在这篇快速文章中,我们已经了解了@Order注解。我们可以在各种用例中发现@Order的应用–在这些用例中,自动连接的组件的顺序很重要。一个例子是Spring的请求过滤器。

Due to its influence on injection precedence, it may seem like it might influence the singleton startup order also. But in contrast, the dependency relationships and @DependsOn declarations determine the singleton startup order.

由于它对注入优先级的影响,它似乎也会影响单子的启动顺序。但相反,依赖关系和@DependsOn声明决定了单子的启动顺序。

All examples mentioned in this tutorial can be found over on Github.

本教程中提到的所有例子都可以在Github上找到over