Change default order of AutoConfigureOrder to 0

Previously, AutoConfigureOrder defaulted to Ordered.LOWEST_PRECEDENCE.
This made is impossible for an individual auto-configuration to
indicate that it wanted to go "last", i.e. after any
auto-configuration classes that didn't not specify an order, or
specified an order other than LOWEST_PRECEDENCE.

This commit changes to default to 0, allowing a single
 auto-configuration to easily indicate that it should go last.

 Closes gh-10142
This commit is contained in:
Andy Wilkinson 2017-09-01 13:14:24 +01:00
parent f49741e3ed
commit 8df852bf71
3 changed files with 17 additions and 8 deletions

View File

@ -26,7 +26,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.springframework.core.Ordered;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.classreading.MetadataReaderFactory;
@ -164,11 +163,11 @@ class AutoConfigurationSorter {
private int getOrder() { private int getOrder() {
if (wasProcessed()) { if (wasProcessed()) {
return this.autoConfigurationMetadata.getInteger(this.className, return this.autoConfigurationMetadata.getInteger(this.className,
"AutoConfigureOrder", Ordered.LOWEST_PRECEDENCE); "AutoConfigureOrder", AutoConfigureOrder.DEFAULT_ORDER);
} }
Map<String, Object> attributes = getAnnotationMetadata() Map<String, Object> attributes = getAnnotationMetadata()
.getAnnotationAttributes(AutoConfigureOrder.class.getName()); .getAnnotationAttributes(AutoConfigureOrder.class.getName());
return (attributes == null ? Ordered.LOWEST_PRECEDENCE return (attributes == null ? AutoConfigureOrder.DEFAULT_ORDER
: (Integer) attributes.get("value")); : (Integer) attributes.get("value"));
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2015 the original author or authors. * Copyright 2012-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -38,11 +38,13 @@ import org.springframework.core.annotation.Order;
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD }) @Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
public @interface AutoConfigureOrder { public @interface AutoConfigureOrder {
int DEFAULT_ORDER = 0;
/** /**
* The order value. Default is {@link Ordered#LOWEST_PRECEDENCE}. * The order value. Default is {@code 0}.
* @see Ordered#getOrder() * @see Ordered#getOrder()
* @return the order value * @return the order value
*/ */
int value() default Ordered.LOWEST_PRECEDENCE; int value() default DEFAULT_ORDER;
} }

View File

@ -40,9 +40,12 @@ import static org.mockito.Mockito.mock;
* Tests for {@link AutoConfigurationSorter}. * Tests for {@link AutoConfigurationSorter}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson
*/ */
public class AutoConfigurationSorterTests { public class AutoConfigurationSorterTests {
private static final String DEFAULT = OrderUnspecified.class.getName();
private static final String LOWEST = OrderLowest.class.getName(); private static final String LOWEST = OrderLowest.class.getName();
private static final String HIGHEST = OrderHighest.class.getName(); private static final String HIGHEST = OrderHighest.class.getName();
@ -86,8 +89,8 @@ public class AutoConfigurationSorterTests {
@Test @Test
public void byOrderAnnotation() throws Exception { public void byOrderAnnotation() throws Exception {
List<String> actual = this.sorter List<String> actual = this.sorter
.getInPriorityOrder(Arrays.asList(LOWEST, HIGHEST)); .getInPriorityOrder(Arrays.asList(LOWEST, HIGHEST, DEFAULT));
assertThat(actual).containsExactly(HIGHEST, LOWEST); assertThat(actual).containsExactly(HIGHEST, DEFAULT, LOWEST);
} }
@Test @Test
@ -193,6 +196,11 @@ public class AutoConfigurationSorterTests {
return StringUtils.collectionToCommaDelimitedString(items); return StringUtils.collectionToCommaDelimitedString(items);
} }
@AutoConfigureOrder
public static class OrderUnspecified {
}
@AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE) @AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
public static class OrderLowest { public static class OrderLowest {