spring @Order标记
@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标记的更多相关文章
- 浅谈Spring @Order注解的使用(转)
注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: 1 ...
- Spring @Order注解的使用
注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响: 1 ...
- Spring实战学习笔记之SpEL表达式
在Spring XML配置文件中装配Bean的属性和构造参数都是静态的,而在运行期才知道装配的值,就可以使用SpEL实现 SpEL表达式的首要目标是通过计算获得某个值. ...
- SpringCloud入门之应用程序上下文服务(Spring Cloud Context)详解
构建分布式系统非常复杂且容易出错.Spring Cloud为最常见的分布式系统模式提供了简单易用的编程模型,帮助开发人员构建弹性,可靠和协调的应用程序.Spring Cloud构建于Spring Bo ...
- Spring版本功能变更&Spring4.x的新特性
有朋友想知道Spring不同版本都有哪些功能变更,说直接在百度搜索找到的结果都不是想要的,其实还是关键词不对,找Spring不同版本的新特性就能获得更好的结果.其实在Spring工程github的wi ...
- Spring 源码阅读 二
程序入口: 接着上一篇博客中看完了在AnnotationConfigApplicationContext的构造函数中的register(annotatedClasses);将我们传递进来的主配置类添加 ...
- Spring基础——AOP通知
spring(AOP通知) 切面 切面是封装通用业务逻辑的组件,可以作用到其他组件上.是spring组件中的某个方法.无返回类型.参数类型与通知类型有关.一个切面 开启数据库 关闭数据库 开启事务 检 ...
- Spring Cloud教程(十)自定义引导配置属性源
可以通过在org.springframework.cloud.bootstrap.BootstrapConfiguration键下添加条目/META-INF/spring.factories来训练引导 ...
- Spring Project Annotations
Project Annotation Discovered By Package Target(s) Parameters Notes . AspectJ @EnableSpr ...
随机推荐
- oradim新建服务后,登录数据库报ORA-12560错误
> oradim -new -sid mydb 实例已创建. > sqlplus /nolog SQL*Plus: Release 11.2.0.4.0 Production on 星期二 ...
- ORA-214 signalled during: ALTER DATABASE MOUNT 问题
数据库服务器移动了位置,后来再连其数据库发现提示无法连接,大致为“无法为所有新实例创建连接...”,查看alert日志发现有如下错误: starting up 1 dispatcher(s) for ...
- lua -- 系统提示框
-- -- Author: chentong -- Date: 2014-3-24 -- 系统提示: -- 先做个简单的,在中上位置出现提示,逐渐消失,如果有新提示,则直接删除旧提示. -- 规则: ...
- python 列表排序方法sort、sorted技巧篇
Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列. 1)排序基础 简单的升序排序是非常容易的.只需要调用sorte ...
- sql操作总结
SQL 语句的多表查询方式例如:按照 department_id 查询 employees(员工表)和 departments(部门表)的信息.方式一(通用型):SELECT ... FROM ... ...
- ZeroMQ总结
Socket types Thread safety: ZeroMQ的socket是非线程安全的,并且ZeroMQ本身不建议在多个线程中传递同一个Socket,即使保证了线程同步. Socket ...
- Windows 8(64位)如何搭建 Android 开发环境与真机测试(转)
可以参考http://wenku.baidu.com/link?url=ghU6IFS1WJXLFKfM_0efv9YQEnMDBrdi9CXwirSs5IOLLeUfdIOh8OOVv0DX89Lt ...
- 安卓开发笔记——Notification通知栏
当用户有没有接到的电话的时候,Android顶部状态栏里就会出现一个小图标.提示用户有没有处理的快讯,当拖动状态栏时,可以查看这些快讯.Android给我们提供了NotificationManager ...
- make INSTALL_MOD_PATH=path_dir modules_install
The INSTALL_MOD_PATH variable is needed to install the modules in the target root filesystem instead ...
- 使用Sublime Text搭建python调试环境[转]
pycharmt等IDE虽然用着爽,但毕竟在速度.资源上还是比较让人不爽的. 使用IDE无非是图个方便省事,特别是像我这种有些记性差的来说. IDE说起来方便于的几个地方就是: 1.语法颜色高亮 2. ...