Refactored support for @Order on @Bean methods as well as @Priority handling

Issue: SPR-11310
Issue: SPR-10548
This commit is contained in:
Juergen Hoeller
2014-09-04 00:41:13 +02:00
parent 82f8b4330c
commit c6d29f1a31
17 changed files with 284 additions and 664 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,8 +43,27 @@ public class OrderComparator implements Comparator<Object> {
public static final OrderComparator INSTANCE = new OrderComparator();
/**
* Build an adapted order comparator with the given soruce provider.
* @param sourceProvider the order source provider to use
* @return the adapted comparator
* @since 4.1
*/
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);
if (p1 && !p2) {
@@ -55,20 +74,62 @@ public class OrderComparator implements Comparator<Object> {
}
// Direct evaluation instead of Integer.compareTo to avoid unnecessary object creation.
int i1 = getOrder(o1);
int i2 = getOrder(o2);
int i1 = getOrder(o1, sourceProvider);
int i2 = getOrder(o2, sourceProvider);
return (i1 < i2) ? -1 : (i1 > i2) ? 1 : 0;
}
/**
* Determine the order value for the given object.
* <p>The default implementation checks against the {@link Ordered}
* interface. Can be overridden in subclasses.
* <p>The default implementation checks against the given {@link OrderSourceProvider}
* using {@link #findOrder} and falls back to a regular {@link #getOrder(Object)} call.
* @param obj the object to check
* @return the order value, or {@code Ordered.LOWEST_PRECEDENCE} as fallback
*/
private int getOrder(Object obj, OrderSourceProvider sourceProvider) {
Integer order = null;
if (sourceProvider != null) {
order = findOrder(sourceProvider.getOrderSource(obj));
}
return (order != null ? order : getOrder(obj));
}
/**
* Determine the order value for the given object.
* <p>The default implementation checks against the {@link Ordered} interface
* through delegating to {@link #findOrder}. Can be overridden in subclasses.
* @param obj the object to check
* @return the order value, or {@code Ordered.LOWEST_PRECEDENCE} as fallback
*/
protected int getOrder(Object obj) {
return (obj instanceof Ordered ? ((Ordered) obj).getOrder() : Ordered.LOWEST_PRECEDENCE);
Integer order = findOrder(obj);
return (order != null ? order : Ordered.LOWEST_PRECEDENCE);
}
/**
* Find an order value indicated by the given object.
* <p>The default implementation checks against the {@link Ordered} interface.
* Can be overridden in subclasses.
* @param obj the object to check
* @return the order value, or {@code null} if none found
*/
protected Integer findOrder(Object obj) {
return (obj instanceof Ordered ? ((Ordered) obj).getOrder() : null);
}
/**
* Determine a priority value for the given object, if any.
* <p>The default implementation always returns {@code null}.
* Subclasses may override this to give specific kinds of values a
* 'priority' characteristic, in addition to their 'order' semantics.
* A priority indicates that it may be used for selecting one object over
* another, in addition to serving for ordering purposes in a list/array.
* @param obj the object to check
* @return the priority value, or {@code null} if none
* @since 4.1
*/
public Integer getPriority(Object obj) {
return null;
}
@@ -115,4 +176,22 @@ public class OrderComparator implements Comparator<Object> {
}
}
/**
* Strategy interface to provide an order source for a given object.
* @since 4.1
*/
public static interface OrderSourceProvider {
/**
* Return an order source for the specified object, i.e. an object that
* should be checked for an order value as a replacement to the given object.
* <p>If the returned object does not indicate any order, the comparator
* will fall back to checking the original object.
* @param obj the object to find an order source for
* @return the order source for that object, or {@code null} if none found
*/
Object getOrderSource(Object obj);
}
}

View File

@@ -16,19 +16,20 @@
package org.springframework.core.annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
/**
* {@link java.util.Comparator} implementation that checks
* {@link org.springframework.core.Ordered} as well as the
* {@link Order} annotation, with an order value provided by an
* {@code Ordered} instance overriding a statically defined
* annotation value (if any).
* {@link java.util.Comparator} implementation that checks Spring's
* {@link org.springframework.core.Ordered} interface as well as the
* {@link Order} annotation and the {@link javax.annotation.Priority}
* annotation, with an order value provided by an {@code Ordered}
* instance overriding a statically defined annotation value (if any).
*
* @author Juergen Hoeller
* @author Oliver Gierke
@@ -36,6 +37,7 @@ import org.springframework.core.Ordered;
* @since 2.0.1
* @see org.springframework.core.Ordered
* @see Order
* @see javax.annotation.Priority
*/
public class AnnotationAwareOrderComparator extends OrderComparator {
@@ -45,16 +47,55 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();
@Override
protected int getOrder(Object obj) {
if (obj instanceof Ordered) {
return ((Ordered) obj).getOrder();
/**
* This implementation checks for the {@link Order} annotation
* on various kinds of elements, in addition to the
* {@link org.springframework.core.Ordered} check in the superclass.
*/
protected Integer findOrder(Object obj) {
// Check for regular Ordered interface
Integer order = super.findOrder(obj);
if (order != null) {
return order;
}
if (obj != null) {
Class<?> clazz = (obj instanceof Class ? (Class<?>) obj : obj.getClass());
return OrderUtils.getOrder(clazz, Ordered.LOWEST_PRECEDENCE);
// Check for @Order annotation on various kinds of elements
if (obj instanceof Class) {
return OrderUtils.getOrder((Class) obj);
}
return Ordered.LOWEST_PRECEDENCE;
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) {
return OrderUtils.getOrder(obj.getClass());
}
return null;
}
/**
* This implementation checks retrieves a {@link javax.annotation.Priority}
* value, allowing for additional semantics over the regular {@link Order}
* annotation: typically, selecting one object over another in case of
* multiple matches but only one object to be returned.
*/
public Integer getPriority(Object obj) {
if (obj instanceof Class) {
return OrderUtils.getPriority((Class) obj);
}
else if (obj != null) {
return OrderUtils.getPriority(obj.getClass());
}
return null;
}

View File

@@ -1,71 +0,0 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* A default {@link OrderProviderComparator} implementation that uses the
* value provided by the {@link OrderProvider} and fallbacks to
* {@link AnnotationAwareOrderComparator} if none is set.
*
* <p>This essentially means that the value of the {@link OrderProvider}
* takes precedence over the behavior of {@link AnnotationAwareOrderComparator}
*
* @author Stephane Nicoll
* @since 4.1
*/
public class DefaultOrderProviderComparator implements OrderProviderComparator {
/**
* Shared default instance of DefaultOrderProviderComparator.
*/
public static final DefaultOrderProviderComparator INSTANCE = new DefaultOrderProviderComparator();
@Override
public void sortList(List<?> items, OrderProvider orderProvider) {
Collections.sort(items, new OrderProviderAwareComparator(orderProvider));
}
@Override
public void sortArray(Object[] items, OrderProvider orderProvider) {
Arrays.sort(items, new OrderProviderAwareComparator(orderProvider));
}
private static class OrderProviderAwareComparator extends AnnotationAwareOrderComparator {
private final OrderProvider orderProvider;
public OrderProviderAwareComparator(OrderProvider orderProvider) {
this.orderProvider = orderProvider;
}
@Override
protected int getOrder(Object obj) {
Integer order = this.orderProvider.getOrder(obj);
if (order != null) {
return order;
}
return super.getOrder(obj);
}
}
}

View File

@@ -1,36 +0,0 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation;
/**
* Strategy interface to provide the order value of a given instance.
*
* @author Stephane Nicoll
* @since 4.1
* @see OrderProviderComparator
*/
public interface OrderProvider {
/**
* Return the order value of the specified object or {@code null} if
* it cannot be retrieved.
* @param obj the object to handle
* @return the order value for that object or {@code null} if none is found
*/
Integer getOrder(Object obj);
}

View File

@@ -1,49 +0,0 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.core.annotation;
import java.util.List;
/**
* Sort a collection of element according to an {@link OrderProvider}.
*
* @author Stephane Nicoll
* @since 4.1
*/
public interface OrderProviderComparator {
/**
* Sort the specified list of items according to their order value,
* using the specified {@link OrderProvider} to retrieve an order
* if necessary.
* @param items the items to sort
* @param orderProvider the order provider to use
* @see java.util.Collections#sort(java.util.List, java.util.Comparator)
*/
void sortList(List<?> items, OrderProvider orderProvider);
/**
* Sort the specified array of items according to their order value,
* using the specified {@link OrderProvider} to retrieve an order
* if necessary.
* @param items the items to sort
* @param orderProvider the order provider to use
* @see java.util.Arrays#sort(Object[], java.util.Comparator)
*/
void sortArray(Object[] items, OrderProvider orderProvider);
}

View File

@@ -38,7 +38,17 @@ public abstract class OrderUtils {
/**
* Return the order on the specified {@code type} or the specified
* Return the order on the specified {@code type}.
* <p>Take care of {@link Order @Order} and {@code @javax.annotation.Priority}.
* @param type the type to handle
* @return the order value, or {@code null} if none can be found
*/
public static Integer getOrder(Class<?> type) {
return getOrder(type, null);
}
/**
* Return the order on the specified {@code type}, or the specified
* default value if none can be found.
* <p>Take care of {@link Order @Order} and {@code @javax.annotation.Priority}.
* @param type the type to handle
@@ -49,7 +59,7 @@ public abstract class OrderUtils {
if (order != null) {
return order.value();
}
Integer priorityOrder = getPriorityValue(type);
Integer priorityOrder = getPriority(type);
if (priorityOrder != null) {
return priorityOrder;
}
@@ -62,7 +72,7 @@ public abstract class OrderUtils {
* @param type the type to handle
* @return the priority value if the annotation is set, {@code null} otherwise
*/
public static Integer getPriorityValue(Class<?> type) {
public static Integer getPriority(Class<?> type) {
if (priorityPresent) {
for (Annotation annotation : type.getAnnotations()) {
if (PRIORITY_ANNOTATION_CLASS_NAME.equals(annotation.annotationType().getName())) {