天天看点

Spring4.0系列7-Ordering Autowired Collections

Spring4.0系列7-Ordering Autowired Collections

import org.springframework.stereotype.component;  

@component  

public class employee implements person {  

}  

Spring4.0系列7-Ordering Autowired Collections

import org.springframework.stereotype.component;   

@component   

public class customer implements person {   

Spring4.0系列7-Ordering Autowired Collections

import org.springframework.beans.factory.annotation.autowired;  

public class organization {  

    @autowired  

    list<person> people;  

    public string tostring() {  

        return people.tostring();  

    }  

此例中,organization中的people是无序的。多数情况下,在xml配置里,bean被有序添加到people list。这时srping 4.0提供了一个解决方案:使用@order。

@order注解在spring2.0时已经在spring框架里。它的主要作用是给组件排序。现在在spring4.0里,它也能给注入到有序的colletion的bean排序。@order接受一个排序值,值小的优先级高,也意味着在collection中排序靠前。上面的例子改写成:

Spring4.0系列7-Ordering Autowired Collections

import org.springframework.core.annotation.order;  

@order(value=1)  

Spring4.0系列7-Ordering Autowired Collections

@order(value=2)  

public class customer implements person {  

}