第二章 一节spring-core之comparator深入解读

原创
2018/04/18 15:32
阅读数 1.6K

前言

本章节深入讲解spring的comparator,使用与细节,帮助大家在项目里面正确使用comparator。源码解读无法通过文字很详细的解释,诠释。需要读者多次阅读,深入理解,组织逻辑,大脑慢慢形成整个流程。

从深入理解java api中的comparator,在脑海中形成一个技术蓝图

private Comparator<Integer> comparator = new Comparator<Integer>() {
	    @Override
	    public int compare(Integer o1, Integer o2) {
			if(o1 == o2){
				return 0;
			}
		return o1 > o2?1:-1;
	    }
	};

public void comparatorTest(){
	Random random = new Random();
	List<Integer> intList = new ArrayList<>();
	for(int i = 0 ; i< 20 ; i++){
	    intList.add(random.nextInt(40));
	}
	log.info("intList里面的数据,现在是无序的:" + intList);
	
	Collections.sort(intList, comparator);
	log.info("排序之后的结果:" + intList.toString());
}

结果

2018-04-01 11:27:44.455 [main] INFO  c.n.b.spring.core.ordere.OrdereTest - intList里面的数据,现在是无序的:[26, 21, 18, 35, 25, 14, 15, 17, 15, 13, 37, 15, 35, 29, 13, 19, 32, 15, 19, 12]
2018-04-01 11:27:44.479 [main] INFO  c.n.b.spring.core.ordere.OrdereTest - 排序之后的结果:[12, 13, 13, 14, 15, 15, 15, 15, 17, 18, 19, 19, 21, 25, 26, 29, 32, 35, 35, 37]

  1. 需要comparator的实现类
  2. 实例化comparator的子类
  3. 保存实例化结果
  4. 调用Collections.sort(intList, comparator);触发排序

流程分析

按照上面comparator的流程,在spring中寻找对应的流程。

需要comparator的实现类,与基本体系

spring 里面comparator的有两个子类分别是OrderComparator,AnnotationAwareOrderComparator 输入图片说明

实例化comparator的子类

comparator的子类都会在当前类里面声明一个该类的静态不可变的变量

public class OrderComparator implements Comparator<Object> {
	public static final OrderComparator INSTANCE = new OrderComparator();
}
public class AnnotationAwareOrderComparator extends OrderComparator {
	public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();
}

保存实例化结果

在DefaultListableBeanFactory里面声明了一个Comparator类型的dependencyComparator变量,用于对bean进行排序

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
		implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
		
	/** Optional OrderComparator for dependency Lists and arrays */
	private Comparator<Object> dependencyComparator;
}

一旦使用AnnotatedBeanDefinitionReader与ClassPathBeanDefinitionScanner,就会加载AnnotationAwareOrderComparator

public class AnnotationConfigUtils {
	public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
			BeanDefinitionRegistry registry, Object source) {

		DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
		if (beanFactory != null) {
			if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
				beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
			}
		}
	.....
	}
}

调用Collections.sort(intList, comparator)触发排序

对BeanFactoryPostProcessor及子类的list接口进行排序

class PostProcessorRegistrationDelegate {
	private static void sortPostProcessors(ConfigurableListableBeanFactory beanFactory, List<?> postProcessors) {
		Comparator<Object> comparatorToUse = null;
		if (beanFactory instanceof DefaultListableBeanFactory) {
			comparatorToUse = ((DefaultListableBeanFactory) beanFactory).getDependencyComparator();
		}
		if (comparatorToUse == null) {
			comparatorToUse = OrderComparator.INSTANCE;
		}
		Collections.sort(postProcessors, comparatorToUse);
	}
}

在调用getBean,如果是返回结果是List或者是Array.就会进行排序

private Object resolveMultipleBeans(DependencyDescriptor descriptor, String beanName,
			Set<String> autowiredBeanNames, TypeConverter typeConverter) {

		Class<?> type = descriptor.getDependencyType();
		if (type.isArray()) {
			Class<?> componentType = type.getComponentType();
			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(), type);
			if (getDependencyComparator() != null && result instanceof Object[]) {
				Arrays.sort((Object[]) result, adaptDependencyComparator(matchingBeans));//这里会进行排序
			}
			return result;
		}
		else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
			Class<?> elementType = descriptor.getCollectionType();
			if (elementType == null) {
				return null;
			}
			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 (getDependencyComparator() != null && result instanceof List) {
				Collections.sort((List<?>) result, adaptDependencyComparator(matchingBeans));//这里会进行排序
			}
			return result;
		}
		else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
			
		}
		else {
			return null;
		}
	}

comparator分析

OrderComparator的实现非常简单,按照compare--> doCompare --> getOrder(Object , OrderSourceProvider)--> getOrder(Object )-->findOrder() 循序查看就好了

输入图片说明

OrderComparator核心代码

public class OrderComparator implements Comparator<Object> {

	/**
	 * Shared default instance of {@code OrderComparator}.
	 */
	public static final OrderComparator INSTANCE = new OrderComparator();


	public Comparator<Object> withSourceProvider(final OrderSourceProvider sourceProvider) {
		return new Comparator<Object>() {
			@Override
			public int compare(Object o1, Object o2) {
				return doCompare(o1, o2, sourceProvider);
			}
		};
	}

	@Override
	public int compare(Object o1, Object o2) {
		return doCompare(o1, o2, null);
	}

	private int doCompare(Object o1, Object o2, OrderSourceProvider sourceProvider) {
		boolean p1 = (o1 instanceof PriorityOrdered);
		boolean p2 = (o2 instanceof PriorityOrdered);
		//  如果一个PriorityOrdered实现,一个是Ordered实现,PriorityOrdered实现优先Ordered实现
		if (p1 && !p2) {
			return -1;
		}
		else if (p2 && !p1) {
			return 1;
		}

		// 当两个对象都是PriorityOrdered或者Ordered,得到order值,进行比较
		int i1 = getOrder(o1, sourceProvider);
		int i2 = getOrder(o2, sourceProvider);
		return (i1 < i2) ? -1 : (i1 > i2) ? 1 : 0;
	}

	private int getOrder(Object obj, OrderSourceProvider sourceProvider) {
		Integer order = null;
		if (sourceProvider != null) {
			Object orderSource = sourceProvider.getOrderSource(obj);
			if (orderSource != null && orderSource.getClass().isArray()) {
				Object[] sources = ObjectUtils.toObjectArray(orderSource);
				for (Object source : sources) {
					order = findOrder(source);
					if (order != null) {
						break;
					}
				}
			}
			else {
				order = findOrder(orderSource);
			}
		}
		return (order != null ? order : getOrder(obj));
	}

	protected int getOrder(Object obj) {
		Integer order = findOrder(obj);// findOrder是核心,会被子类重写,
		return (order != null ? order : Ordered.LOWEST_PRECEDENCE);
	}
	/**
	 * 注意这个方法会被子类重写。主要是调用Ordered实现类 实现的getOrder方法,得到orderd值
	 **/
	protected Integer findOrder(Object obj) {
		return (obj instanceof Ordered ? ((Ordered) obj).getOrder() : null);
	}

 	/**
	 * 注意这个方法会被子类重写
	 **/
	public Integer getPriority(Object obj) {
		return null;
	}
}

AnnotationAwareOrderComparator核心代码

public class AnnotationAwareOrderComparator extends OrderComparator {

	public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();

	protected Integer findOrder(Object obj) {
		// 调用OrderComparator.findOrder,先识别Ordered接口,
		Integer order = super.findOrder(obj);
		if (order != null) {
			return order;
		}

		// 检查@Order和@Priority各种元素
		// Method,AnnotatedElement的对象只会识别@Order
		if (obj instanceof Class) {
			return OrderUtils.getOrder((Class<?>) obj);
		}
		else if (obj instanceof Method) {
			Order ann = AnnotationUtils.findAnnotation((Method) obj, Order.class);
			if (ann != null) {
				return ann.value();
			}
		}
		else if (obj instanceof AnnotatedElement) {
			Order ann = AnnotationUtils.getAnnotation((AnnotatedElement) obj, Order.class);
			if (ann != null) {
				return ann.value();
			}
		}
		else if (obj != null) {
			order = OrderUtils.getOrder(obj.getClass());
			if (order == null && obj instanceof DecoratingProxy) {
				order = OrderUtils.getOrder(((DecoratingProxy) obj).getDecoratedClass());
			}
		}

		return order;
	}


	public Integer getPriority(Object obj) {
		Integer priority = null;
		if (obj instanceof Class) {
			priority = OrderUtils.getPriority((Class<?>) obj);
		}
		else if (obj != null) {
			priority = OrderUtils.getPriority(obj.getClass());
			if (priority == null && obj instanceof DecoratingProxy) {
				priority = OrderUtils.getOrder(((DecoratingProxy) obj).getDecoratedClass());
			}
		}
		return priority;
	}

}

OrderUtils

OrderUtils 很简单负责从class中识别 @Priority 与 @Order注解

public abstract class OrderUtils {

	private static Class<? extends Annotation> priorityAnnotationType = null;

	static {
		try {
			// 加载 @Priority 注解,
			priorityAnnotationType = (Class<? extends Annotation>)
					ClassUtils.forName("javax.annotation.Priority", OrderUtils.class.getClassLoader());
		}
		catch (Throwable ex) {
			// javax.annotation.Priority not available, or present but not loadable (on JDK 6)
		}
	}


	public static Integer getOrder(Class<?> type) {
		return getOrder(type, null);
	}

	
	public static Integer getOrder(Class<?> type, Integer defaultOrder) {
		// @Order 优于 @Priority 识别
		Order order = AnnotationUtils.findAnnotation(type, Order.class);
		if (order != null) {
			return order.value();
		}
		// 识别 @Priority 注释
		Integer priorityOrder = getPriority(type);
		if (priorityOrder != null) {
			return priorityOrder;
		}
		return defaultOrder;
	}

	
	public static Integer getPriority(Class<?> type) {
		if (priorityAnnotationType != null) {
			Annotation priority = AnnotationUtils.findAnnotation(type, priorityAnnotationType);
			if (priority != null) {
				return (Integer) AnnotationUtils.getValue(priority);
			}
		}
		return null;
	}

}

总结

Ordered相关对象总结

  1. 默认具有以下接口与注解的类才会进行排序
    1. PriorityOrdered
    2. Ordered
    3. @Order
    4. @Priority
  2. OrderComparator负责PriorityOrdered,Ordered
  3. AnnotationAwareOrderComparator负责@Order, @Priority
  4. AnnotationAwareOrderComparator只会在xml开启action扫描或者使用 AnnotatedBeanDefinitionReader与ClassPathBeanDefinitionScanner,才会加载

只会对同类型的对象进行排序

  1. 对BeanFactoryPostProcessor,及子类进行排序
  2. 在调用getBean,如果是返回结果是List或者是Array.就会进行排序
  3. ConfigurationClassParser.processDeferredImportSelectors 对@Impor排序
  4. AspectJ 也就是aop

识别优先级

同时存在多个排序实现方式,哪个方式的值作为排序的值。Ordered优选级比@Order,@Order优选级@Priority高

@Order(value = -10000)
@Priority ( value = 0 )
public class OrderType implements Ordered{

	@Override
	public int getOrder( ) {
		return 10000;
	}
}

上面的代码,如何通过AnnotationAwareOrderComparato.findOrde()得到的值是10000,如果没有实现Ordered接口,得到是值是-1000

排序优先级

	@Test
	public void orderTypeTest(){
		List<Ordered> list = new ArrayList< Ordered >();
		list.add( new OrderType( ) );
		list.add( new PriorityOrderedType() );
		
		Collections.sort(list , AnnotationAwareOrderComparator.INSTANCE);
		System.out.println( list );
	}
	
	public class OrderType implements Ordered{

		@Override
		public int getOrder( ) {
			return -1;
		}
		
		public String toString(){
			return "Ordered";
		}
	}
	
	
	public class PriorityOrderedType implements PriorityOrdered{

		@Override
		public int getOrder( ) {
			return 10000;
		}
		public String toString(){
			return "PriorityOrderedType";
		}
		
	}

[PriorityOrderedType, Ordered]

从上面的代码,与结果会发现,PriorityOrdered的实现类order值是10000,而Ordered的实现类order值是-10000,理论上来说输出结果是[Ordered , PriorityOrderedType],为什么出现这种情况。因为PriorityOrdered接口排序优选Ordered接口(包括@Order, @Priority)

展开阅读全文
加载中

作者的其它热门文章

打赏
0
1 收藏
分享
打赏
0 评论
1 收藏
0
分享
返回顶部
顶部