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. nginx的安装 、Nginx默认虚拟主机、nginx用户认证、nginx 域名重定向

    1.nginx:官网:www.nginx.org 下载:wget -c  http://nginx.org/download/nginx-1.14.0.tar.gz解压:tar -zxvf nginx ...

  2. Hadoop错误:java.io.IOException: Incompatible clusterIDs

    问题: 配置Hadoop集群时,一个节点的DataNode无法启动 排查: 查看hadoop-root-datanode-bigdata114.log文件,错误信息如下: java.io.IOExce ...

  3. Python下载与安装配置

    最近想学习Python那就开始吧 首先就是下载安装了 Python最新源码,二进制文档,新闻资讯等可以在Python的官网查看到: Python官网:https://www.python.org/ 你 ...

  4. 【点击项目属性】Target runtime Apache Tomcat v7.0 is not defined

    这个项目是去年用同一个eclipse做的,但是今年原封不动导入的时候,确发现这个东西 tomcat选成我们本地的tomcat8就可以了 泼菲解决.

  5. linux杀死进程方法

    方法一: 根据pid ps -aux|grep 你的进程 方法二: 删除pid文件 [root@localhost ~]# /etc/init.d/redis_6380 start /var/run/ ...

  6. H3C路由器映射端口到外网

    登录路由器web管理端 选择高级设置->虚拟服务器->新增虚拟服务器

  7. 快速挂载iso文件到虚拟机系统

    在vm软件菜单栏那里选择vm,再选择弹出菜单最下面的设置,如图,找到实体机上的iso文件,保存. 这时候,在虚拟机ls /dev会发现有一个cdrom,这个就是我们的iso文件,不过我们还需要把它挂载 ...

  8. Java打印水仙花数

    public class Test2 { public static void main(String[] args) { //水仙花 数 指的是一个三位数(100-999) //三位数本身= 百位数 ...

  9. Gravitational Teleport docker-compose简单运行

    Gravitational Teleport 可以作为堡垒机进行使用,为了测试方便使用docker-compose 运行一个all in one 的demo 备注: 官方提供的docker-compo ...

  10. 全网最详细的Windows系统里Oracle 11g R2 Client(64bit)的下载与安装(图文详解)

    不多说,直接上干货! 环境: windows10系统(64位) 最好先安装jre或jdk(此软件用来打开oracle自带的可视化操作界面,不装也没关系:可以安装plsql,或者直接用命令行操作) Or ...