@Order标记定义了组件的加载顺序。

@Order标记从spring 2.0出现,但是在spring 4.0之前,@Order标记只支持AspectJ的切面排序。spring 4.0对@Order做了增强,它开始支持对装载在诸如Lists和Arrays容器中的自动包装(auto-wired)组件的排序。

在spring内部,对基于spring xml的应用,spring使用OrderComparator类来实现排序。对基于注解的应用,spring采用AnnotationAwareOrderComparator来实现排序。

@Order 标记定义如下:

@Retention(value=RUNTIME)
@Target(value={TYPE,METHOD,FIELD})
@Documented
public @interface Order 这个标记包含一个value属性。属性接受整形值。如:1,2 等等。值越小拥有越高的优先级。
下面让我们来看一个使用spring 3.x 和spring 4.x 的例子:

Ranks.java

package com.javapapers.spring3.autowire.collection;

public interface Ranks {

}

RankOne.java

package com.javapapers.spring3.autowire.collection;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Component
public class RankOne implements Ranks{ private String rank = "RankOne"; public String toString(){
return this.rank;
} }

RankTwo.java

package com.javapapers.spring3.autowire.collection;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Component
public class RankTwo implements Ranks{ private String rank = "RankTwo"; public String toString(){
return this.rank;
}
}

RankThree.java

package com.javapapers.spring3.autowire.collection;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Component
public class RankThree implements Ranks{ private String rank = "RankThree"; public String toString(){
return this.rank;
}
}

Results.java

Component to hold student ranks in a collection as shown below.

package com.javapapers.spring3.autowire.collection;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class Results {
@Autowired
private List ranks ; @Override
public String toString(){
return ranks.toString();
}
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <context:component-scan base-package="com.javapapers.spring3"/>
</beans>

RanksClient.java

package com.javapapers.spring3.autowire.collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class RanksClient {
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Results results = (Results)context.getBean("results");
System.out.println(results);
} } 输出结果:
[RankOne, RankThree, RankTwo]

从结果可以看出,结果是没有顺序的。因为spring 3.x不支持对自动包装组件的排序。

接下来,我升级spring的版本至4.x, 并对RanOne,RankTwo和RankThree加上order标记,值做相应的调整。
@Component
@Order(1)
public class RankOne implements Ranks{
private String rank = "RankOne";
    
    public String toString(){
        return this.rank;
    }
} 输出结果如下:
[RankOne, RankTwo, RankThree]
参考文章: http://javapapers.com/spring/spring-order-annotation/ 
 

spring @Order标记的更多相关文章

  1. 浅谈Spring @Order注解的使用(转)

    注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: 1 ...

  2. Spring @Order注解的使用

    注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: 1 ...

  3. Spring实战学习笔记之SpEL表达式

            在Spring XML配置文件中装配Bean的属性和构造参数都是静态的,而在运行期才知道装配的值,就可以使用SpEL实现         SpEL表达式的首要目标是通过计算获得某个值. ...

  4. SpringCloud入门之应用程序上下文服务(Spring Cloud Context)详解

    构建分布式系统非常复杂且容易出错.Spring Cloud为最常见的分布式系统模式提供了简单易用的编程模型,帮助开发人员构建弹性,可靠和协调的应用程序.Spring Cloud构建于Spring Bo ...

  5. Spring版本功能变更&Spring4.x的新特性

    有朋友想知道Spring不同版本都有哪些功能变更,说直接在百度搜索找到的结果都不是想要的,其实还是关键词不对,找Spring不同版本的新特性就能获得更好的结果.其实在Spring工程github的wi ...

  6. Spring 源码阅读 二

    程序入口: 接着上一篇博客中看完了在AnnotationConfigApplicationContext的构造函数中的register(annotatedClasses);将我们传递进来的主配置类添加 ...

  7. Spring基础——AOP通知

    spring(AOP通知) 切面 切面是封装通用业务逻辑的组件,可以作用到其他组件上.是spring组件中的某个方法.无返回类型.参数类型与通知类型有关.一个切面 开启数据库 关闭数据库 开启事务 检 ...

  8. Spring Cloud教程(十)自定义引导配置属性源

    可以通过在org.springframework.cloud.bootstrap.BootstrapConfiguration键下添加条目/META-INF/spring.factories来训练引导 ...

  9. Spring Project Annotations

       Project  Annotation  Discovered By  Package     Target(s)  Parameters  Notes . AspectJ @EnableSpr ...

随机推荐

  1. CSS3 选择器 基本选择器介绍

    CSS是一种用于屏幕上渲染html,xml等一种语言,CSS主要是在相应的元素中应用样式,来渲染相对应用的元素,那么这样我们选择相应的元素就很重要了,如何选择对应的元素,此时就需要我们所说的选择器.选 ...

  2. shiny: Web Application Framework for R

    shiny: Web Application Framework for R 基于R语言的一个web框架,适用于数据分析与图表绘画展示类型的网站.

  3. 基于mindwave脑电波进行疲劳检测算法的设计(5)

    时隔两个多月了,前段时间在弄Socket,就没有弄这个了.现在好了,花了几天的时间,终于又完成了一小部分了.这一小节主要讲α,β,δ,θ等等波段之间的关系.废话不多说,直接给出这几天的成果. 上一次, ...

  4. Node入门教程(8)第六章:path 模块详解

    path 模块详解 path 模块提供了一些工具函数,用于处理文件与目录的路径.由于windows和其他系统之间路径不统一,path模块还专门做了相关处理,屏蔽了彼此之间的差异. 可移植操作系统接口( ...

  5. java框架篇---hibernate(一对多)映射关系

    一对多关系可以分为单向和双向. 一对多关系单向 单向就是只能从一方找到另一方,通常是从主控类找到拥有外键的类(表).比如一个母亲可以有多个孩子,并且孩子有母亲的主键作为外键.母亲与孩子的关系就是一对多 ...

  6. 【九天教您南方cass 9.1】02 从地形图上绘制纵横断面

    同学们大家好,欢迎收看由老王测量上班记出品的cass9.1视频课程, 测量空间的[九天教您南方cass]专栏是九天老师专门开设cass免费教学班.希望能帮助那些刚入行的同学,并祝您一臂之力. [点击索 ...

  7. TI am335x am437x PRU

    http://bbs.eeworld.com.cn/thread-355798-1-1.html

  8. Java 继承中构造方法的执行顺序问题

    在Java中,如果一个类没有任何显式创建的构造器则该类默认会有一个无参构造器:如果显式创建了有参构造器则该类就不再有默认无参构造器. 在Java继承中,构造器并不能被继承,而是被显示或隐式调用. 1. ...

  9. Git -- 使用GitHub

    我们一直用GitHub作为免费的远程仓库,如果是个人的开源项目,放到GitHub上是完全没有问题的.其实GitHub还是一个开源协作社区,通过GitHub,既可以让别人参与你的开源项目,也可以参与别人 ...

  10. [Localization] SSD - Single Shot MultiBoxDetector

    Prerequisite: VGG Ref: [Object Tracking] Localization and Detection SSD Paper: http://lib.csdn.net/a ...