ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

@Order in Spring Spring框架中的@Order注解

2022-03-18 22:03:03  阅读:192  来源: 互联网

标签:Spring order 注解 getRating Order public


原文链接:@Order in Spring

1. Overview 简述

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注解,它用来对被注解标记的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_PROCEDENCE 用于标记来说明当前组件具有最高的优先级。

2. When to Use @Order 什么时候使用@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.

在Spring4.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.

从Spring4.0以后,开始支持把组件注入到集合中,所以呢,应付按照同一类型做为分级依据,把它的多个实现类作为集合元素,@Order注解的值作为索引顺序。

Let's explore it with a quick example. 接下来看看如何使用吧

3. How to Use @Order 如何使用@Order注解

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

首先,创建项目以及测试@Order注解所需的接口类与多个子类。

3.1. Interface Creation 创建接口类

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

创建一个决定商品税率的接口类 Rating

public interface Rating {
    int getRating();
}

3.2. Components Creation 创建它的子类组件

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的商品具有最低的优先级,决定这一点的是@Order注解中的值。

4. Testing Our Example 测试示例程序

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 最后

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.

根据@Order注解在部件及注入的影响作用,看似它能影响到单例的启动顺序。但是,恰恰相反,@DependsOn注解决定单例的顺序,而非本文讲述的@Order注解。

大家多多关注点赞哈

标签:Spring,order,注解,getRating,Order,public
来源: https://blog.csdn.net/u011225581/article/details/123401515

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有