`
wiselyman
  • 浏览: 2080806 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81077
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130130
社区版块
存档分类
最新评论

Spring4.0系列7-Ordering Autowired Collections

 
阅读更多

Spring4.0系列1-新特性

Spring4.0系列2-环境搭建

Spring4.0系列3-@RestController

Spring4.0系列4-Meta Annotation(元注解)

Spring4.0系列5-@Conditional 

Spring4.0系列6-Generic Qualifier(泛型限定)

Spring4.0系列7-Ordering Autowired Collections

Spring4.0系列8-Groovy DSL

Spring4.0系列9-websocket简单应用

更多正在编写中。。。

 

 

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

import org.springframework.stereotype.Component;
@Component
public class Employee implements Person {
}

 

import org.springframework.stereotype.Component; 
@Component 
public class Customer implements Person { 
}

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
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中排序靠前。上面的例子改写成:
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(value=1)
public class Employee implements Person {
}
 
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(value=2)
public class Customer implements Person {
}
 

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics