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

  1. import org.springframework.stereotype.Component;
  2. @Component
  3. public class Employee implements Person {
  4. }
  1. import org.springframework.stereotype.Component;
  2. @Component
  3. public class Customer implements Person {
  4. }
  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. public String toString() {
  8. return people.toString();
  9. }
  10. }

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

@Order注解在Spring2.0时已经在Spring框架里。它的主要作用是给组件排序。现在在Spring4.0里,它也能给注入到有序的colletion的bean排序。@Order接受一个排序值,值小的优先级高,也意味着在collection中排序靠前。上面的例子改写成:
  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. }
  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. }

spring4.0之七:Ordering Autowired Collections的更多相关文章

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

    http://wiselyman.iteye.com/blog/2003336 ******************************************* Spring4.0系列1-新特性 ...

  2. [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

    原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版 ...

  3. spring4.0之三:@RestController

    spring4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解.4.0之前的版本,Spring MVC的组件都使用@Controller来标识当前类是一个 ...

  4. 【Spring实战-2】Spring4.0.4整合Hibernate4.3.6

    作者:ssslinppp      源程序下载:http://download.csdn.net/detail/ssslinppp/8751185  1. 摘要 本文主要讲解如何在Spring4.0. ...

  5. Spring框架入门之Spring4.0新特性——泛型注入

    Spring框架入门之Spring4.0新特性——泛型注入 一.为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入. ...

  6. Spring4.0编程式定时任务配置

    看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...

  7. Spring4.0支持Groovy配置

    介绍 前一段时间观注了一下Spring4.0的一些特性,当中就有对Groovy配置的支持.因为临时还没有很深入的研究.所以举个小样例来说明一下怎样支持Groovy配置. package shuai.s ...

  8. Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用]

    Spring4.0.1+Quartz2.2.1实现定时任务调度[亲测可用] tip:只需要配置xml文件即可 1.第三方依赖包的引入 <properties> <project.bu ...

  9. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口

    Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.介绍RESTful架构 ...

随机推荐

  1. 行级锁 java||数据库

    http://www.cnblogs.com/xiyubaby/p/4623516.html select * from t for update 会等待行锁释放之后,返回查询结果. select * ...

  2. 【git】一台机器上使用不同的git账号

    1.生成一个新的自定义名称的公钥: ssh-keygen -t rsa -C "shangxiaofei3@163.com" -f ~/.ssh/sxfself 一直点击回车 执行 ...

  3. 微信公众号文章转语音tts

    微信公众号里面的文章在走路或者开车时候不方便浏览,希望能增加一个文字转语音功能,那么问题来了,到底哪家文字转语音技术强呢? 经过验证,目前发现最好用的还是balabolka ,国内的什么“录音啦”,试 ...

  4. tessellate Architecture

    转自:https://zalando-incubator.github.io/tessellate/ Tessellate consists of two major components: the ...

  5. Spring AOP + PageHelper分页

    1.增加依赖配置 <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweav ...

  6. UML图类,接口之间的关系

    UML图类之间的关系(来自大话设计模式中的一张图,代表了类,接口之间的各种关系)

  7. cocos 3.16 生成预编译库模板

    打开模板功能 不知道什么原因,cocos 3.16关闭了模板功能,因此使用cocos -h命令时,无法显示gen-templates参数. 打开的方式也挺简单,打开tools/cocos2d-cons ...

  8. 12c R2 RAC Oracle Linux 7.3 ESXI6.5

    环境:ESXI6.5虚拟化 主机配置:操作系统 Oracle Linux 7.3 CPU:8个VCPU 内存:16G 本地磁盘:50G 全程默认最小化安装Oracle Linux 7.3操作系统 每个 ...

  9. Digispark kickstarter + JoyStick 模拟鼠标

    IDE:Arduino 1.0.4 一.线路连接 S-Y --> P5(A0) S-X --> P2(A1) S-K --> P0 VCC --> VCC GND --> ...

  10. GBT27930-2015电动汽车非车载传导式充电机与电池管理系统之间的通信协议

    本标准规定了电动汽车非车载传导式充电机(简称充电机)与电池管理系统(Battery Management System,简称BMS)之间基于控制器局域网(Control Area NetWork,简称 ...