java 豆子的Spring有序列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16967971/
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-11-01 00:33:19  来源:igfitidea点击:

Spring ordered list of beans

javaspringsortingspring-mvcioc-container

提问by Jordi P.S.

I have several beans that implement the same interface. Each bean is annotated with

我有几个实现相同接口的 bean。每个 bean 都带有注释

@Component 
@Order(SORT_ORDER).
public class MyClass implements BeanInterface{
    ...
}

At one point I autowire a list of components and I expect a sorted list of beans. The list of beans is not sorted according the orders I have set with the annotation.

有一次,我自动装配了一个组件列表,我希望得到一个已排序的 bean 列表。bean 列表没有按照我在注释中设置的顺序进行排序。

I tried implementing the interface Ordered and the same behaviour occurs.

我尝试实现接口 Ordered 并且发生了相同的行为。

@Component
public class Factory{


    @Autowired
    private List<BeanInterface> list; // <- I expect a sorted list here
    ...
}

Am I doing anything wrong?

我做错了什么吗?

回答by Jordi P.S.

I found a solution to the issue, as you say, this annotation is not meant for that despite it would be a nice feature.

我找到了该问题的解决方案,正如您所说,尽管该注释是一个不错的功能,但它并不适用于此。

To make it work this way its just necessary to add the following code in the bean containing the sorted list.

要使其以这种方式工作,只需在包含排序列表的 bean 中添加以下代码即可。

@PostConstruct
public void init() {
    Collections.sort(list,AnnotationAwareOrderComparator.INSTANCE);
}

Hope it helps.

希望能帮助到你。

回答by rubensa

Ordering autowired collections is supported since Spring 4.

从 Spring 4 开始支持订购自动装配的集合。

See: Spring 4 Ordering Autowired Collections

请参阅:Spring 4 订购自动装配的集合

Summary: if you add @Order(value=1), @Order(value=2)... to your bean definitions, they will be injected in a collection ordered according to the valueparameter. This is not the same as declaring that you want the collection in natural order- for that you have to explicitly sort the list yourself after receiving it, as per Jordi P.S.'s answer.

总结:如果你将@Order(value=1), @Order(value=2)...添加到你的 bean 定义中,它们将被注入到一个根据value参数排序的集合中。这与声明您想要按自然顺序排列的集合不同- 因为根据Jordi PS 的回答,您必须在收到列表后自己明确地对列表进行排序。

回答by clav

The @Orderannotation is used to specify the order in which AOP advice is executed, it doesn't sort lists. To achieve sorting on your list have your BeanInterfaceclasses implement the Comparableinterface and override the compareTomethod to specify how the objects should be sorted. Then you can sort the list using Collections.sort(list). Assuming BeanInterfacehas a method called getSortOrderthat returns an Integerobject specifying the object's sort order, you could do something like this:

@Order注释用于指定在其中执行AOP通知顺序,它不会排序名单。要在您的列表中实现排序,让您的BeanInterface类实现Comparable接口并覆盖该compareTo方法以指定应如何对对象进行排序。然后您可以使用 对列表进行排序Collections.sort(list)。假设BeanInterface有一个调用的方法getSortOrder返回一个Integer指定对象排序顺序的对象,您可以执行以下操作:

@Component 
public class MyClass implements BeanInterface, Comparable<BeanInterface> {
    public Integer getSortOrder() {
        return sortOrder;
    }

    public int compareTo(BeanInterface other) {
        return getSortOrder().compareTo(other.getSortOrder());
    }
}

Then you can sort the list like this:

然后你可以像这样对列表进行排序:

Collections.sort(list);

回答by Martin Frey

There is a jira issue about that feature in spring. I have added an implementation of beanfactory in the comment which im currently using to support that feature:

春季有一个关于该功能的 jira 问题。我在评论中添加了 beanfactory 的实现,我目前使用它来支持该功能:

https://jira.springsource.org/browse/SPR-5574

https://jira.springsource.org/browse/SPR-5574