天天看點

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 {  

}