spring版本為5.0.11
注意:注解@Order或者接口Ordered的作用是定義Spring IOC容器中Bean的執行順序的優先級,而不是定義Bean的加載順序,Bean的加載順序不受@Order或Ordered接口的影響;
Ordered接口,用來排序的。
Spring是一個大量使用策略設計模式的框架,這意味着有很多相同接口的實現類,那么必定會有優先級的問題。
於是,Spring就提供了Ordered這個接口,來處理相同接口實現類的優先級問題。
getOrder()值越小,優先級越高。
PriorityOrdered是個接口,繼承自Ordered接口,未定義任何方法
OrderComparator比較器進行排序的時候,若2個對象中有一個對象實現了PriorityOrdered接口,那么這個對象的優先級更高。
若2個對象都是PriorityOrdered或Ordered接口的實現類,那么比較Ordered接口的getOrder方法得到order值,值越低,優先級越高。
@Order
1.spring4.2可以用@Order控制配置類的加載順序
2.常用於aop優先事務執行
3.@Order不標注數字,默認最低優先級,即int的最大值
Ordered接口的源碼
/** * {@code Ordered} is an interface that can be implemented by objects that * should be <em>orderable</em>, for example in a {@code Collection}. * * <p>The actual {@link #getOrder() order} can be interpreted as prioritization, * with the first object (with the lowest order value) having the highest * priority. * * <p>Note that there is also a <em>priority</em> marker for this interface: * {@link PriorityOrdered}. Consult the Javadoc for {@code PriorityOrdered} for * details on how {@code PriorityOrdered} objects are ordered relative to * <em>plain</em> {@link Ordered} objects. * * <p>Consult the Javadoc for {@link OrderComparator} for details on the * sort semantics for non-ordered objects. * * @author Juergen Hoeller * @author Sam Brannen * @since 07.04.2003 * @see PriorityOrdered * @see OrderComparator * @see org.springframework.core.annotation.Order * @see org.springframework.core.annotation.AnnotationAwareOrderComparator */ public interface Ordered { /** * Useful constant for the highest precedence value. * @see java.lang.Integer#MIN_VALUE */ int HIGHEST_PRECEDENCE = Integer.MIN_VALUE; /** * Useful constant for the lowest precedence value. * @see java.lang.Integer#MAX_VALUE */ int LOWEST_PRECEDENCE = Integer.MAX_VALUE; /** * Get the order value of this object. * <p>Higher values are interpreted as lower priority. As a consequence, * the object with the lowest value has the highest priority (somewhat * analogous to Servlet {@code load-on-startup} values). * <p>Same order values will result in arbitrary sort positions for the * affected objects. * @return the order value * @see #HIGHEST_PRECEDENCE * @see #LOWEST_PRECEDENCE */ int getOrder(); }
@Order源碼
/** * {@code @Order} defines the sort order for an annotated component. * * <p>The {@link #value} is optional and represents an order value as defined in the * {@link Ordered} interface. Lower values have higher priority. The default value is * {@code Ordered.LOWEST_PRECEDENCE}, indicating lowest priority (losing to any other * specified order value). * * <p><b>NOTE:</b> Since Spring 4.0, annotation-based ordering is supported for many * kinds of components in Spring, even for collection injection where the order values * of the target components are taken into account (either from their target class or * from their {@code @Bean} method). While such order values may influence priorities * at injection points, please be aware that they do not influence singleton startup * order which is an orthogonal concern determined by dependency relationships and * {@code @DependsOn} declarations (influencing a runtime-determined dependency graph). * * <p>Since Spring 4.1, the standard {@link javax.annotation.Priority} annotation * can be used as a drop-in replacement for this annotation in ordering scenarios. * Note that {@code @Priority} may have additional semantics when a single element * has to be picked (see {@link AnnotationAwareOrderComparator#getPriority}). * * <p>Alternatively, order values may also be determined on a per-instance basis * through the {@link Ordered} interface, allowing for configuration-determined * instance values instead of hard-coded values attached to a particular class. * * <p>Consult the javadoc for {@link org.springframework.core.OrderComparator * OrderComparator} for details on the sort semantics for non-ordered objects. * * @author Rod Johnson * @author Juergen Hoeller * @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}) @Documented public @interface Order { /** * The order value. * <p>Default is {@link Ordered#LOWEST_PRECEDENCE}. * @see Ordered#getOrder() */ int value() default Ordered.LOWEST_PRECEDENCE; }