@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. IDEA使用笔记(八)——自动生成 serialVersionUID 的设置

    这个设置比较简单,也有一些博文已经写到了,为什么我还要写哪?(潜台词:因为我想凑一篇博文)我觉得学习,特别是编程学习是需要重复造轮子的,另外,就是加深自己的印象方便自己的查找.还有就是关键点,有些博客 ...

  2. Lambert漫反射的BRDF

    Lambert漫反射brdf=Albedo/pi. 推导: 如图,设Lambert面元获得入射照度Ei,各方向均匀释放亮度,每个方向释放的亮度都是Lo. 又设此表面反射率为Albedo,根据反射率定义 ...

  3. Atitit 计算word ppt文档的页数

    Atitit 计算word ppt文档的页数 http://localhost:8888/ http://git.oschina.net/attilax/ati_wordutil private vo ...

  4. win8使用技巧

    windows 8操作系统相信大家已经不再陌生了,虽然正式版本还未发布,但不少朋友已经在使用微软事先推出的windows 消费者预览版,直白的说就是公测版,预览版是免费的,但仅可以使用一年,但其功能与 ...

  5. 物联网架构成长之路(10)-Nginx负载均衡

    0. 前言 关于Nginx负载均衡的简单配置,我以前博客配置过基于HTTP的负载均衡.这次的负载均衡有点不一样,就是基于TCP的负载均衡.基于HTTP负载均衡是默认的Nginx版本支持的,配置也很简单 ...

  6. netbeans增加yii的代码提示

    在NetBeans中创建PHP项目 NetBeans目前还没有直接支持Yii Framework或Yii相关的插件,但是可以通过其他方式来实现Yii的代码自动提示. 首先,打开新建项目向导,创建一个“ ...

  7. hdoj:2037

    #include <iostream> using namespace std; struct Time { int start; int end; }; Time times[]; ]; ...

  8. 解决:ngxin做http强制跳转https,接口的POST请求变成GET

    域名配置了http强制跳转htpps后发现app发起post请求会出现405错误. 所以怀疑是http强制跳转https出现了问题.修改nginx配置如下即可解决: server { listen ; ...

  9. 【转帖】Mysql多维数据仓库指南 第一篇 第1章

     Mysql多维数据仓库指南 第一篇基本原理 章节列表: 第1章:基本组成 第2章:维度历史 第3章:维度可加性 第4章:维度查询 本篇概述 你将运用关系数据库来实施一个维度数据仓库.事实表和维表这两 ...

  10. 假期小结 BIO, NIO, AIO

    虽然忙碌,但仍小有收获,开心. 引子 BIO: Blocking IO,阻塞式IO NIO: Non-blocking IO,非阻塞式IO AIO: Async IO,异步IO 问题 什么是阻塞式IO ...