• 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. }  
  • 相关阅读:
    第一次作业
    1-10的四则运算
    实验九
    实验五
    实验四
    实验三
    实验二
    实验一
    汇编第一章总结
    实验九
  • 原文地址:https://www.cnblogs.com/duanxz/p/7491195.html
Copyright © 2020-2023  润新知