zoukankan      html  css  js  c++  java
  • spring4.0之七:Ordering Autowired Collections

    Spring 4.0的一个小特性是在自动注入的时候使用@Order。Spring 2.5中,我们将bean注入List,如下代码:

    Java代码  收藏代码
    1. import org.springframework.stereotype.Component;  
    2. @Component  
    3. public class Employee implements Person {  
    4. }  
    Java代码  收藏代码
    1. import org.springframework.stereotype.Component;   
    2. @Component   
    3. public class Customer implements Person {   
    4. }  
    Java代码  收藏代码
    1. import org.springframework.beans.factory.annotation.Autowired;  
    2. import org.springframework.stereotype.Component;  
    3. @Component  
    4. public class Organization {  
    5.     @Autowired  
    6.     List<Person> people;  
    7.    
    8.     public String toString() {  
    9.         return people.toString();  
    10.     }  
    11. }  
    此例中,Organization中的people是无序的。多数情况下,在xml配置里,bean被有序添加到people list。这时Srping 4.0提供了一个解决方案:使用@Order。
    @Order注解在Spring2.0时已经在Spring框架里。它的主要作用是给组件排序。现在在Spring4.0里,它也能给注入到有序的colletion的bean排序。@Order接受一个排序值,值小的优先级高,也意味着在collection中排序靠前。上面的例子改写成:
    Java代码  收藏代码
    1. import org.springframework.core.annotation.Order;  
    2. import org.springframework.stereotype.Component;  
    3. @Component  
    4. @Order(value=1)  
    5. public class Employee implements Person {  
    6. }  
     
    Java代码  收藏代码
    1. import org.springframework.core.annotation.Order;  
    2. import org.springframework.stereotype.Component;  
    3. @Component  
    4. @Order(value=2)  
    5. public class Customer implements Person {  
    6. }  
  • 相关阅读:
    DFS——CodeForces740DAlyona and a tree
    DFS——hdu1016Prime Ring Problem
    DFS(8)——poj2034Anti-prime Sequences
    DFS(7)——poj1011Sticks
    DFS(2)——hdu1241Oil Deposits
    DFS(6)——hdu1342Lotto
    NO12——快速幂取模
    NO11——01背包
    NO10——各种欧几里得
    NO9——线段相关
  • 原文地址:https://www.cnblogs.com/duanxz/p/7491195.html
Copyright © 2011-2022 走看看