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. Atcoder 1973:こだわり者いろはちゃん / Iroha's Obsession

    C - こだわり者いろはちゃん / Iroha's Obsession Time limit : 2sec / Memory limit : 256MB Score : 300 points Prob ...

  2. HDU 1171 Big Event in HDU dp背包

    Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s ...

  3. poj1504 Adding Reversed Numbers

    Adding Reversed Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17993 Accepted: 9 ...

  4. Autofac解耦事件总线

    事件总线之Autofac解耦 事件总线是通过一个中间服务,剥离了常规事件的发布与订阅(消费)强依赖关系的一种技术实现.事件总线的基础知识可参考圣杰的博客[事件总线知多少] 本片博客不再详细概述事件总线 ...

  5. 尚硅谷【SpringBoot】web(源码讲解太多不建议阅读)

    四.Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可 ...

  6. Cassandra -- Cassandra 3.0版本安装

    ============================================================ 服务器信息 搭建三节点的Cassandra群集: SERVER1: 192.1 ...

  7. Gravitational Teleport简单使用

    使用官方提供的二进制包进行快速启动测试,详细细节还需要在学习 下载软件包 mac 系统 https://gravitational.com/teleport/download/ wget https: ...

  8. Linux内核设计基础(三)之定时器和时间管理

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/BlueCloudMatrix/article/details/29294529 内核知道连续两次时钟 ...

  9. oracle-rman-2

    归档日志的备份 RMAN> list archivelog all;show archivelog deletion policy;configure archivelog deletion p ...

  10. oracle故障解决

    修改了字符集,修改错了,然后不能启动 alter system set nls_language='AMERICA'; shutdown immediate; startup 报错 [oracle@o ...