spring Spring中@Order注解有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30328897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:35:02  来源:igfitidea点击:

What is the use of @Order annotation in Spring?

springspring-securityannotations

提问by Qasim

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

我看到了使用@Order注释的代码一目了然。我想知道这个注解对于 Spring Security 或 Spring MVC 有什么用处。

Here is an example:

下面是一个例子:

@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?

如果我们不使用这个注解,上面提到的类的顺序会发生什么?

回答by Patrik Bego

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.

最高优先级的建议最先运行。数字越小,优先级越高。例如,给定两条 'before' 通知,具有最高优先级的将首先运行。

Another way of using it is for ordering Autowired collections

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

@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

你可以在这里找到可执行代码:https: //github.com/patrikbego/spring-order-demo.git

Hope this clarifies it a little bit further.

希望这能进一步澄清它。

Output:-

输出:-

Mazda Toyota

马自达丰田

回答by Scorpio

@OrderAnnotations (as well as the Orderedinterface) implies a specific order, in which the beans will be loaded or prioritized by Spring.

@Order注解(以及Ordered接口)意味着一个特定的顺序,其中 bean 将被 Spring 加载或确定优先级。

Lower numbers indicate a higher priority. The feature may be used to add beans in a specific order into a collection (ie via @Autowired), among other things.

较低的数字表示较高的优先级。该功能可用于以特定顺序将 bean 添加到集合中(即通过@Autowired),等等。

In your specific example the annotation does not change anything in the class itself. Whereever this specific class is used, it is used with the highest priority (since it is set as '1'), probably since additional, but depending information is being added in other classes, ordered at a lower precedence.

在您的具体示例中,注释不会更改类本身的任何内容。无论在何处使用此特定类,它都以最高优先级使用(因为它被设置为“1”),可能是因为其他类中添加了其他但依赖信息,以较低的优先级排序。

回答by Praful Jha

@Order Annotation specifies the order of loading the bean by spring container. lower the order(integer) , higher is the precedence. So order of 0 will have more precedence than order of 10. Likewise order of -100 will have more precedence than 0.

@Order 注解指定 spring 容器加载 bean 的顺序。order(integer) 越低,优先级越高。因此,0 的顺序将比 10 的顺序具有更高的优先级。同样,-100 的顺序将比 0 具有更高的优先级。