Spring中@Order注释的用途是什么? [英] What is the use of @Order annotation in Spring?

查看:318
本文介绍了Spring中@Order注释的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一眼使用@Order注释的代码.我想知道相对于Spring Security或Spring MVC,此注释的用途是什么.

I have come across a glance of code which uses @Order annotation. I want to know what is the use of this annotation with respect to Spring Security or Spring MVC.

这里是一个例子:

@Order(1)
public class StatelessAuthenticationSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private TokenAuthenticationService tokenAuthenticationService;

}

如果不使用此批注,上述类的顺序会怎样?

What happen to the order of above mentioned class if we do not use this annotation?

推荐答案

它用于咨询执行优先级.

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())
        }
    }
}

您可以在此处找到可执行代码: https://github.com/patrikbego/spring -order-demo.git

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

希望这可以进一步澄清它.

Hope this clarifies it a little bit further.

输出:-

马自达丰田

这篇关于Spring中@Order注释的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆