javax.annotation.Priority alternative to @Order
This commit rationalizes the use of @Order so that the standard @Priority annotation can be used instead. The handling of both annotations are now defined in OrderUtils. This also updates the link to the JavaEE API so that we refer to JavaEE7 instead of JavaEE6. Issue: SPR-11639
This commit is contained in:
@@ -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.
|
||||
@@ -32,6 +32,7 @@ import org.springframework.core.Ordered;
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Oliver Gierke
|
||||
* @author Stephane Nicoll
|
||||
* @since 2.0.1
|
||||
* @see org.springframework.core.Ordered
|
||||
* @see Order
|
||||
@@ -51,10 +52,7 @@ public class AnnotationAwareOrderComparator extends OrderComparator {
|
||||
}
|
||||
if (obj != null) {
|
||||
Class<?> clazz = (obj instanceof Class ? (Class<?>) obj : obj.getClass());
|
||||
Order order = AnnotationUtils.findAnnotation(clazz, Order.class);
|
||||
if (order != null) {
|
||||
return order.value();
|
||||
}
|
||||
return OrderUtils.getOrder(clazz, Ordered.LOWEST_PRECEDENCE);
|
||||
}
|
||||
return Ordered.LOWEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.
|
||||
@@ -29,6 +29,9 @@ import org.springframework.core.Ordered;
|
||||
* The default value is {@code Ordered.LOWEST_PRECEDENCE}, indicating
|
||||
* lowest priority (losing to any other specified order value).
|
||||
*
|
||||
* <p>Since Spring 4.1, the standard {@link javax.annotation.Priority} can be used as
|
||||
* a drop-in replacement of this annotation.
|
||||
*
|
||||
* <p><b>NOTE:</b> Annotation-based ordering is supported for specific kinds of
|
||||
* components only, e.g. for annotation-based AspectJ aspects. Spring container
|
||||
* strategies, on the other hand, are typically based on the {@link Ordered}
|
||||
@@ -39,6 +42,8 @@ import org.springframework.core.Ordered;
|
||||
* @since 2.0
|
||||
* @see org.springframework.core.Ordered
|
||||
* @see AnnotationAwareOrderComparator
|
||||
* @see OrderUtils
|
||||
* @see javax.annotation.Priority
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.lang.annotation.Annotation;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* General utility for determining the order of an object based
|
||||
* on its type declaration.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 4.1
|
||||
* @see Order
|
||||
* @see javax.annotation.Priority
|
||||
*/
|
||||
public abstract class OrderUtils {
|
||||
|
||||
private static final String PRIORITY_ANNOTATION_CLASS_NAME = "javax.annotation.Priority";
|
||||
|
||||
private static final boolean priorityPresent =
|
||||
ClassUtils.isPresent(PRIORITY_ANNOTATION_CLASS_NAME, OrderUtils.class.getClassLoader());
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @return the priority value of the default if none can be found
|
||||
*/
|
||||
public static Integer getOrder(Class<?> type, Integer defaultOrder) {
|
||||
Order order = AnnotationUtils.findAnnotation(type, Order.class);
|
||||
if (order != null) {
|
||||
return order.value();
|
||||
}
|
||||
Integer priorityOrder = getPriorityValue(type);
|
||||
if (priorityOrder != null) {
|
||||
return priorityOrder;
|
||||
}
|
||||
return defaultOrder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the value of the {@code javax.annotation.Priority} annotation set on the
|
||||
* specified type or {@code null} if none is set.
|
||||
* @param type the type to handle
|
||||
* @return the priority value if the annotation is set, {@code null} otherwise
|
||||
*/
|
||||
public static Integer getPriorityValue(Class<?> type) {
|
||||
if (priorityPresent) {
|
||||
for (Annotation annotation : type.getAnnotations()) {
|
||||
if (PRIORITY_ANNOTATION_CLASS_NAME.equals(annotation.annotationType().getName())) {
|
||||
return (Integer) AnnotationUtils.getValue(annotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user