Spring @Order註解使用詳解
前言
很長一段時間沒有寫博客瞭,今天一時心血來潮,突然寫出這篇文章就很突兀。但是看到網上關於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 內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Spring中如何使用Comparator接口
- 關於Spring的@Autowired依賴註入常見錯誤的總結
- Spring之什麼是ObjectFactory?什麼是ObjectProvider?
- springboot bean循環依賴實現以及源碼分析
- Spring筆記-@Order註解和Ordered接口解析