Spring @Order注解使用详解

 更新时间:2022年08月15日 15:35:39   作者:l昨日青天  
注解@Order或者接口Ordered的作用是定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序,Bean的加载顺序不受@Order或Ordered接口的影响

前言

很长一段时间没有写博客了,今天一时心血来潮,突然写出这篇文章就很突兀。但是看到网上关于Spring的@Order注解的不是特别准确的结论,想着还是表达一下看法,当然也是通过写文章来让自己的思路更清晰一点,如果有不是很赞同的部分,希望可以一起讨论。

首先先说结论:Spring的@Order注解(或者实现Ordered接口、或者实现PriorityOrdered接口),不决定Bean的实例化顺序和执行顺序,更不会影响Spring的Bean的扫描顺序;它影响着Spring将扫描的多个Bean放入数组、集合(Map)时的排序。

下面我只验证@Order注解,至于实现接口的方式有兴趣的可以试一下。

例子一

@Slf4j(topic = "e")
@Order(1)
@Component
public class OrderTestService1 {
	public OrderTestService1(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
}
@Slf4j(topic = "e")
@Order(2)
@Component
public class OrderTestService2 {
	public OrderTestService2(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
}
@Slf4j(topic = "e")
@Order(3)
@Component
public class OrderTestService3 {
	public OrderTestService3(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
}

打印顺序如下:
OrderTestService1.java 行数=13 10:56:20.756 [main] DEBUG e - order-1
OrderTestService2.java 行数=13 10:56:20.760 [main] DEBUG e - order-2
OrderTestService3.java 行数=13 10:56:20.761 [main] DEBUG e - order-3

例子二

改变OrderTestService三个类的注解序值

@Slf4j(topic = "e")
@Order(3)
@Component
public class OrderTestService1 {
	public OrderTestService1(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}

}
@Slf4j(topic = "e")
@Order(2)
@Component
public class OrderTestService2 {
	public OrderTestService2(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
}
@Slf4j(topic = "e")
@Order(1)
@Component
public class OrderTestService3 {
	public OrderTestService3(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
}

结果:当改变OrderTestService接口的三个类注解序值时,类的实例化顺序根本没有变化,即@Order注解不决定Bean的实例化顺序。

例子三

@Slf4j(topic = "e")
public class E {
	public void orderList(){
		int orderValue = 0;
		if(this.getClass().isAnnotationPresent(Order.class)){
			Order order = this.getClass().getAnnotation(Order.class);
			orderValue = order.value();
		}
		log.debug("List Order postProcessBeanFactory {} order={}",this.getClass().getSimpleName(),orderValue);
	}
}
@Slf4j(topic = "e")
@Order(3)
@Component
public class OrderTestService1 extends E{
	public OrderTestService1(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
}
@Slf4j(topic = "e")
@Order(2)
@Component
public class OrderTestService2 extends E{
	public OrderTestService2(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
}
@Slf4j(topic = "e")
@Order(1)
@Component
public class OrderTestService3 extends E{
	public OrderTestService3(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
}
@Component
public class OrderTestService {
	List<E> beanFactoryPostProcessor;
	public List<E> getBeanFactoryPostProcessor() {
		return beanFactoryPostProcessor;
	}
	@Autowired
	public void setBeanFactoryPostProcessor(List<E> beanFactoryPostProcessor) {
		this.beanFactoryPostProcessor = beanFactoryPostProcessor;
	}
}

打印顺序如下:
E.java 行数=15 11:01:47.756 [main] DEBUG e - List Order postProcessBeanFactory OrderTestService3 order=1
E.java 行数=15 11:01:47.756 [main] DEBUG e - List Order postProcessBeanFactory OrderTestService2 order=2
E.java 行数=15 11:01:47.756 [main] DEBUG e - List Order postProcessBeanFactory OrderTestService1 order=3

结论:当通过注入类型为集合或者数组(可以自行认证)时,@Order的注解值影响注入的顺序,而这并不代表着说@Order注解影响着Bean的执行顺序,接着往下看。

实例论证

不影响Bean的执行顺序

然上面的三个类全都实现BeanFactoryPostProcessor,然后观察postProcessBeanFactory方法的执行顺序,如下:

@Slf4j(topic = "e")
@Order(3)
@Component
public class OrderTestService1 extends E implements BeanFactoryPostProcessor {
	public OrderTestService1(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		int orderValue = 0;
		if(this.getClass().isAnnotationPresent(Order.class)){
			Order order = this.getClass().getAnnotation(Order.class);
			orderValue = order.value();
		}
		log.debug("execute postProcessBeanFactory a order={}",orderValue);
	}
}
@Slf4j(topic = "e")
@Order(2)
@Component
public class OrderTestService2 extends E implements BeanFactoryPostProcessor {
	public OrderTestService2(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		int orderValue = 0;
		if(this.getClass().isAnnotationPresent(Order.class)){
			Order order = this.getClass().getAnnotation(Order.class);
			orderValue = order.value();
		}
		log.debug("execute postProcessBeanFactory a order={}",orderValue);
	}
}
@Slf4j(topic = "e")
@Order(1)
@Component
public class OrderTestService3 extends E implements BeanFactoryPostProcessor {
	public OrderTestService3(){
		log.debug("order-{}",this.getClass().getAnnotation(Order.class).value());
	}
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		int orderValue = 0;
		if(this.getClass().isAnnotationPresent(Order.class)){
			Order order = this.getClass().getAnnotation(Order.class);
			orderValue = order.value();
		}
		log.debug("execute postProcessBeanFactory a order={}",orderValue);
	}
}

结论:通过上面的打印结果,Spring的@Order注解并不影响Bean的执行顺序。

接下来主要分析为什么@Order注解在注入类型为集合时可以影响其顺序。

源码分析

源码位置:org.springframework.beans.factory.support.DefaultListableBeanFactory#doResolveDependency方法中这一段

@Nullable
	public Object doResolveDependency(DependencyDescriptor descriptor, @Nullable String beanName,
			@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException {
		InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint(descriptor);
		try {
			Object shortcut = descriptor.resolveShortcut(this);
			if (shortcut != null) {
				return shortcut;
			}
			// 获取依赖的类型
			Class<?> type = descriptor.getDependencyType();
			Object value = getAutowireCandidateResolver().getSuggestedValue(descriptor);
			if (value != null) {
				if (value instanceof String) {
					String strVal = resolveEmbeddedValue((String) value);
					BeanDefinition bd = (beanName != null && containsBean(beanName) ?
							getMergedBeanDefinition(beanName) : null);
					value = evaluateBeanDefinitionString(strVal, bd);
				}
				TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
				try {
					return converter.convertIfNecessary(value, type, descriptor.getTypeDescriptor());
				}
				catch (UnsupportedOperationException ex) {
					// A custom TypeConverter which does not support TypeDescriptor resolution...
					return (descriptor.getField() != null ?
							converter.convertIfNecessary(value, type, descriptor.getField()) :
							converter.convertIfNecessary(value, type, descriptor.getMethodParameter()));
				}
			}
			// resolveMultipleBeans方法是解析当前依赖项是否支持多个bean注入 比如list
			// 如果是能支持多个注入则在该方法内部就完成了bean的查找,否则下面完成查找
			Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter);
			if (multipleBeans != null) {
				return multipleBeans;
			}
			// 完成查找的功能,有可能会查找出来多个结果
			// 需要注意的是这里的多个结果和上面的支持的多个注入不是同一回事
			Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
			if (matchingBeans.isEmpty()) {
				// 如果没有找到而且你又在依赖上面加上了必须的条件,则会出异常
				if (isRequired(descriptor)) {
					raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);
				}
				// 如果没有加必须条件则返回null,意思就是不注入任何对象
				return null;
			}
			String autowiredBeanName;
			Object instanceCandidate;
			// 假设找出来多个
			if (matchingBeans.size() > 1) {
				// 通过descriptor 也就是依赖描述器来推断出来需要注入的这个对象的名字
				// 注意这里不是当前注入对象的名字而是需要注入的对象的名字
				// 假设A依赖B,这里推断的B这个对象应该叫什么名字
				autowiredBeanName = determineAutowireCandidate(matchingBeans, descriptor);
				// 假设推断出来为null
				// 什么情况下为null?就是你提供的名字和任何找出来的对象的名字匹配不上
				if (autowiredBeanName == null) {
					if (isRequired(descriptor) || !indicatesMultipleBeans(type)) {
						return descriptor.resolveNotUnique(descriptor.getResolvableType(), matchingBeans);
					}
					else {
						// In case of an optional Collection/Map, silently ignore a non-unique case:
						// possibly it was meant to be an empty collection of multiple regular beans
						// (before 4.3 in particular when we didn't even look for collection beans).
						return null;
					}
				}
				// 获取类 通过名字
				instanceCandidate = matchingBeans.get(autowiredBeanName);
			}
			else {
				// We have exactly one match.
				Map.Entry<String, Object> entry = matchingBeans.entrySet().iterator().next();
				autowiredBeanName = entry.getKey();
				instanceCandidate = entry.getValue();
			}
			if (autowiredBeanNames != null) {
				autowiredBeanNames.add(autowiredBeanName);
			}
			if (instanceCandidate instanceof Class) {
				instanceCandidate = descriptor.resolveCandidate(autowiredBeanName, type, this);
			}
			Object result = instanceCandidate;
			if (result instanceof NullBean) {
				if (isRequired(descriptor)) {
					raiseNoMatchingBeanFound(type, descriptor.getResolvableType(), descriptor);
				}
				result = null;
			}
			if (!ClassUtils.isAssignableValue(type, result)) {
				throw new BeanNotOfRequiredTypeException(autowiredBeanName, type, instanceCandidate.getClass());
			}
			return result;
		}
		finally {
			ConstructorResolver.setCurrentInjectionPoint(previousInjectionPoint);
		}
	}

主要看这一句:Object multipleBeans = resolveMultipleBeans(descriptor, beanName, autowiredBeanNames, typeConverter);

@Nullable
	private Object resolveMultipleBeans(DependencyDescriptor descriptor, @Nullable String beanName,
			@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) {
		Class<?> type = descriptor.getDependencyType();
		if (descriptor instanceof StreamDependencyDescriptor) {
			// findAutowireCandidates 根据类型查询
			Map<String, Object> matchingBeans = findAutowireCandidates(beanName, type, descriptor);
			if (autowiredBeanNames != null) {
				autowiredBeanNames.addAll(matchingBeans.keySet());
			}
			Stream<Object> stream = matchingBeans.keySet().stream()
					.map(name -> descriptor.resolveCandidate(name, type, this))
					.filter(bean -> !(bean instanceof NullBean));
			if (((StreamDependencyDescriptor) descriptor).isOrdered()) {
				stream = stream.sorted(adaptOrderComparator(matchingBeans));
			}
			return stream;
		}
		else if (type.isArray()) {
			Class<?> componentType = type.getComponentType();
			ResolvableType resolvableType = descriptor.getResolvableType();
			Class<?> resolvedArrayType = resolvableType.resolve(type);
			if (resolvedArrayType != type) {
				componentType = resolvableType.getComponentType().resolve();
			}
			if (componentType == null) {
				return null;
			}
			// findAutowireCandidates 根据类型查询
			Map<String, Object> matchingBeans = findAutowireCandidates(beanName, componentType,
					new MultiElementDescriptor(descriptor));
			if (matchingBeans.isEmpty()) {
				return null;
			}
			if (autowiredBeanNames != null) {
				autowiredBeanNames.addAll(matchingBeans.keySet());
			}
			TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
			Object result = converter.convertIfNecessary(matchingBeans.values(), resolvedArrayType);
			if (result instanceof Object[]) {
				Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
				if (comparator != null) {
					Arrays.sort((Object[]) result, comparator);
				}
			}
			return result;
		}
		else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
			Class<?> elementType = descriptor.getResolvableType().asCollection().resolveGeneric();
			if (elementType == null) {
				return null;
			}
			// findAutowireCandidates 根据类型查询
			Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType,
					new MultiElementDescriptor(descriptor));
			if (matchingBeans.isEmpty()) {
				return null;
			}
			if (autowiredBeanNames != null) {
				autowiredBeanNames.addAll(matchingBeans.keySet());
			}
			TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
			Object result = converter.convertIfNecessary(matchingBeans.values(), type);
			if (result instanceof List) {
				if (((List<?>) result).size() > 1) {
					Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
					if (comparator != null) {
						((List<?>) result).sort(comparator);
					}
				}
			}
			return result;
		}
		else if (Map.class == type) {
			ResolvableType mapType = descriptor.getResolvableType().asMap();
			Class<?> keyType = mapType.resolveGeneric(0);
			if (String.class != keyType) {
				return null;
			}
			Class<?> valueType = mapType.resolveGeneric(1);
			if (valueType == null) {
				return null;
			}
			// findAutowireCandidates 根据类型查询
			Map<String, Object> matchingBeans = findAutowireCandidates(beanName, valueType,
					new MultiElementDescriptor(descriptor));
			if (matchingBeans.isEmpty()) {
				return null;
			}
			if (autowiredBeanNames != null) {
				autowiredBeanNames.addAll(matchingBeans.keySet());
			}
			return matchingBeans;
		}
		else {
			return null;
		}
	}

主要看这一段:

		else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
			Class<?> elementType = descriptor.getResolvableType().asCollection().resolveGeneric();
			if (elementType == null) {
				return null;
			}
			// findAutowireCandidates 根据类型查询
			Map<String, Object> matchingBeans = findAutowireCandidates(beanName, elementType,
					new MultiElementDescriptor(descriptor));
			if (matchingBeans.isEmpty()) {
				return null;
			}
			if (autowiredBeanNames != null) {
				autowiredBeanNames.addAll(matchingBeans.keySet());
			}
			TypeConverter converter = (typeConverter != null ? typeConverter : getTypeConverter());
			Object result = converter.convertIfNecessary(matchingBeans.values(), type);
			if (result instanceof List) {
				if (((List<?>) result).size() > 1) {
				    // 调用比较器
					Comparator<Object> comparator = adaptDependencyComparator(matchingBeans);
					if (comparator != null) {
						((List<?>) result).sort(comparator);
					}
				}
			}
			return result;
		}
	@Nullable
	private Comparator<Object> adaptDependencyComparator(Map<String, ?> matchingBeans) {
		// 获取比较器
		Comparator<Object> comparator = getDependencyComparator();
		if (comparator instanceof OrderComparator) {
			return ((OrderComparator) comparator).withSourceProvider(
					createFactoryAwareOrderSourceProvider(matchingBeans));
		}
		else {
			return comparator;
		}
	}
	public Comparator<Object> withSourceProvider(OrderSourceProvider sourceProvider) {
		return (o1, o2) -> doCompare(o1, o2, sourceProvider);
	}
	private int doCompare(@Nullable Object o1, @Nullable Object o2, @Nullable OrderSourceProvider sourceProvider) {
		// 是否实现了PriorityOrdered接口
		boolean p1 = (o1 instanceof PriorityOrdered);
		boolean p2 = (o2 instanceof PriorityOrdered);
		if (p1 && !p2) {
			return -1;
		}
		else if (p2 && !p1) {
			return 1;
		}
		int i1 = getOrder(o1, sourceProvider);
		int i2 = getOrder(o2, sourceProvider);
		return Integer.compare(i1, i2);
	}

接下来比较重要,上面注释了是否实现了PriorityOrdered接口,如果没有则调用getOrder方法,getOrder方法判断是否实现了Order接口,如果没有获取@Order注解值。

	private int getOrder(@Nullable Object obj, @Nullable OrderSourceProvider sourceProvider) {
		Integer order = null;
		if (obj != null && sourceProvider != null) {
			// 拿到实现了Order接口的类
			Object orderSource = sourceProvider.getOrderSource(obj);
			if (orderSource != null) {
				if (orderSource.getClass().isArray()) {
					for (Object source : ObjectUtils.toObjectArray(orderSource)) {
						order = findOrder(source);
						if (order != null) {
							break;
						}
					}
				}
				else {
					order = findOrder(orderSource);
				}
			}
		}
		return (order != null ? order : getOrder(obj));
	}
   protected int getOrder(@Nullable Object obj) {
		if (obj != null) {
			Integer order = findOrder(obj);
			if (order != null) {
				return order;
			}
		}
		return Ordered.LOWEST_PRECEDENCE;
	}
	@Override
	@Nullable
	protected Integer findOrder(Object obj) {
		Integer order = super.findOrder(obj);
		if (order != null) {
			return order;
		}
		return findOrderFromAnnotation(obj);
	}
	@Nullable
	private Integer findOrderFromAnnotation(Object obj) {
		AnnotatedElement element = (obj instanceof AnnotatedElement ? (AnnotatedElement) obj : obj.getClass());
		MergedAnnotations annotations = MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY);
		Integer order = OrderUtils.getOrderFromAnnotations(element, annotations);
		if (order == null && obj instanceof DecoratingProxy) {
			return findOrderFromAnnotation(((DecoratingProxy) obj).getDecoratedClass());
		}
		return order;
	}

总结下来就是:当我们通过构造函数或者set方法注入进某个List<类>时,Spring的DefaultListableBeanFactory类会在注入时获取AnnotationAwareOrderComparator比较器帮助我们对类进行排序,AnnotationAwareOrderComparator是OrderComparator的子类,而OrderComparator实现了比较器Comparator接口。排序的策略会先判断是否实现PriorityOrdered接口,如果没有接着会判断是否实现Order接口,此时也没有就会根据注解值进行比较。

到此这篇关于Spring @Order注解使用详解的文章就介绍到这了,更多相关Spring @Order 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:

相关文章

  • Javaweb使用getPart接收表单文件过程解析

    Javaweb使用getPart接收表单文件过程解析

    这篇文章主要介绍了Javaweb使用getPart接收表单文件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • SpringBoot的HandlerInterceptor中依赖注入为null问题

    SpringBoot的HandlerInterceptor中依赖注入为null问题

    这篇文章主要介绍了SpringBoot的HandlerInterceptor中依赖注入为null问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 修改xml文件再也不用重启项目mybatis-xmlreload方法

    修改xml文件再也不用重启项目mybatis-xmlreload方法

    这篇文章主要为大家介绍了修改xml文件再也不用重启项目mybatis-xmlreload,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • kafka topic 权限控制(设置删除权限)

    kafka topic 权限控制(设置删除权限)

    大家都知道Kafka是一个消息队列,把消息放到队列里边的叫生产者,从队列里边消费的叫消费者。今天通过本文给大家介绍kafka topic 权限控制的相关知识,感兴趣的朋友一起看看吧
    2021-11-11
  • SpringBoot实现缓存预热的几种常用方案

    SpringBoot实现缓存预热的几种常用方案

    缓存预热是指在 Spring Boot 项目启动时,预先将数据加载到缓存系统(如 Redis)中的一种机制,本文给大家介绍了SpringBoot实现缓存预热的几种常用方案,并通过代码示例讲解的非常详细,需要的朋友可以参考下
    2024-02-02
  • Java设计模式之工厂模式案例详解

    Java设计模式之工厂模式案例详解

    工厂模式(Factory Pattern)是Java中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。本文将通过案例详细讲解一下工厂模式,需要的可以参考一下
    2022-02-02
  • Java后台判断ajax请求及处理过程详解

    Java后台判断ajax请求及处理过程详解

    这篇文章主要介绍了Java后台判断ajax请求及处理过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • SpringBoot访问MongoDB数据库的两种方式

    SpringBoot访问MongoDB数据库的两种方式

    MongoDB是一种非关系型数据库,通过文档存储数据,适用于大规模数据存储和高并发访问,这篇文章主要介绍了SpringBoot访问MongoDB数据库的两种方式,感兴趣想要详细了解可以参考下文
    2023-05-05
  • 基于线程、并发的基本概念(详解)

    基于线程、并发的基本概念(详解)

    下面小编就为大家带来一篇基于线程、并发的基本概念(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • Java中Elasticsearch的核心概念详解

    Java中Elasticsearch的核心概念详解

    这篇文章主要介绍了Java中Elasticsearch的核心概念详解,Elasticsearch 是一个分布式、免费和开放的搜索和分析引擎,适用于所有类型的数据,包括文本、数字、地理空间、结构化和非结构化数据,需要的朋友可以参考下
    2023-07-07

最新评论