Spring中@Order注解有什么用? [英] What is the use of @Order annotation in Spring?

查看:29
本文介绍了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?

推荐答案

用于 Advice 执行优先级.

It is used for Advice execution precedence.

最高优先级的建议最先运行.数字越小,优先级越高.例如,给定两条before"通知,优先级最高的一条将首先运行.

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.

另一种使用它的方式是订购 Autowired 集合

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天全站免登陆