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. hdu 1864 最大报销额 01背包

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

  2. PTA——数列求和

    PTA 7-34 求分数序列前N项和 #include<stdio.h> int main() { int i,n; ,fm = ,sum = ; scanf("%d" ...

  3. HTTP/2及HTTP/3特性

    HTTP/2及HTTP/3特性 摘要: 学习 HTTP/2 与 HTTP/3. 前言 HTTP/2 相比于 HTTP/1,可以说是大幅度提高了网页的性能,只需要升级到该协议就可以减少很多之前需要做的性 ...

  4. 无用之flask

    Oldboy s4 Flask   Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收 ...

  5. 《DSP using MATLAB》Problem 5.5

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  6. LeetCode - Reorganize String

    Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...

  7. Mybatis二(高级部分)

    1.输入映射和输出映射 a)        输入参数映射 b)        返回值映射 2.动态sql a)        If标签 b)        Where标签 c)        Sql片 ...

  8. System类的使用

    1.System类: 不能被实例化,调用方式:  System.方法 2.用于计算程序执行的时间,currentTimeMillis()方法 System.currentTimeMillis(): p ...

  9. nginx unit nodejs 模块试用(续)

      最新(应该是18 年了)nginx unit 发布了新的版本,对于nodejs 的支持有很大的改进,上次测试过,问题还是 比较多,这次使用新版本在测试下对于nodejs 的支持,以及以前block ...

  10. 嵌入式LINUX设置时间

    date -s "2018-12-15 08:55:00" 安装NTP从网络获取时间 基于Linux的嵌入式开发,需要用到本地的时间,但是网上找了很多修改时间的命令,但大多是Lin ...