From 5c4b698f8635b4cd9ae31cdd82f9899b0daf6c12 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 24 Feb 2015 14:01:03 -0800 Subject: [PATCH] Support string names @AutoConfigureBefore/After Update @AutoConfigureBefore and @AutoConfigureAfter annotations to support String classnames in addition direct Class references. Fixes gh-2529 --- .../AutoConfigurationSorter.java | 16 +++++----------- .../autoconfigure/AutoConfigureAfter.java | 9 ++++++++- .../autoconfigure/AutoConfigureBefore.java | 9 ++++++++- .../condition/ConditionalOnClass.java | 3 ++- .../AutoConfigurationSorterTests.java | 19 ++++++++++++++++++- 5 files changed, 41 insertions(+), 15 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationSorter.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationSorter.java index cbcf095219..60962a61d1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationSorter.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigurationSorter.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -18,12 +18,10 @@ package org.springframework.boot.autoconfigure; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -56,15 +54,11 @@ class AutoConfigurationSorter { public List getInPriorityOrder(Collection classNames) throws IOException { - final AutoConfigurationClasses classes = new AutoConfigurationClasses( this.metadataReaderFactory, classNames); - List orderedClassNames = new ArrayList(classNames); - // Initially sort alphabetically Collections.sort(orderedClassNames); - // Then sort by order Collections.sort(orderedClassNames, new Comparator() { @Override @@ -74,12 +68,9 @@ class AutoConfigurationSorter { return (i1 < i2) ? -1 : (i1 > i2) ? 1 : 0; } }); - // Then respect @AutoConfigureBefore @AutoConfigureAfter orderedClassNames = sortByAnnotation(classes, orderedClassNames); - return orderedClassNames; - } private List sortByAnnotation(AutoConfigurationClasses classes, @@ -170,7 +161,10 @@ class AutoConfigurationSorter { if (attributes == null) { return Collections.emptySet(); } - return new HashSet(Arrays.asList((String[]) attributes.get("value"))); + Set value = new LinkedHashSet(); + Collections.addAll(value, (String[]) attributes.get("value")); + Collections.addAll(value, (String[]) attributes.get("name")); + return value; } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureAfter.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureAfter.java index 7a65541d25..20be05f57d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureAfter.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureAfter.java @@ -35,6 +35,13 @@ public @interface AutoConfigureAfter { * The auto-configure classes that should have already been applied. * @return the classes */ - Class[] value(); + Class[] value() default {}; + + /** + * The names of the auto-configure classes that should have already been applied. + * @return the class names + * @since 1.2.2 + */ + String[] name() default {}; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureBefore.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureBefore.java index f1d166ceb9..c8de3a50ab 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureBefore.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/AutoConfigureBefore.java @@ -35,6 +35,13 @@ public @interface AutoConfigureBefore { * The auto-configure classes that should have not yet been applied. * @return the classes */ - Class[] value(); + Class[] value() default {}; + + /** + * The names of the auto-configure classes that should have already been applied. + * @return the class names + * @since 1.2.2 + */ + String[] name() default {}; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java index e3c7bc59ca..92d6c45723 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnClass.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2013 the original author or authors. + * Copyright 2012-2015 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. @@ -48,4 +48,5 @@ public @interface ConditionalOnClass { * @return the class names that must be present. */ public String[] name() default {}; + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java index e235c8d831..10595f4733 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationSorterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -51,6 +51,8 @@ public class AutoConfigurationSorterTests { private static final String X = AutoConfigureX.class.getName(); private static final String Y = AutoConfigureY.class.getName(); private static final String Z = AutoConfigureZ.class.getName(); + private static final String A2 = AutoConfigureA2.class.getName(); + private static final String W2 = AutoConfigureW2.class.getName(); @Rule public ExpectedException thrown = ExpectedException.none(); @@ -94,6 +96,13 @@ public class AutoConfigurationSorterTests { assertThat(actual, nameMatcher(C, W, B, A, X)); } + @Test + public void byAutoConfigureMixedBeforeAndAfterWithClassNames() throws Exception { + List actual = this.sorter.getInPriorityOrder(Arrays.asList(A2, B, C, W2, + X)); + assertThat(actual, nameMatcher(C, W2, B, A2, X)); + } + @Test public void byAutoConfigureMixedBeforeAndAfterWithDifferentInputOrder() throws Exception { @@ -160,6 +169,10 @@ public class AutoConfigurationSorterTests { public static class AutoConfigureA { } + @AutoConfigureAfter(name = "org.springframework.boot.autoconfigure.AutoConfigurationSorterTests$AutoConfigureB") + public static class AutoConfigureA2 { + } + @AutoConfigureAfter({ AutoConfigureC.class, AutoConfigureD.class, AutoConfigureE.class }) public static class AutoConfigureB { @@ -179,6 +192,10 @@ public class AutoConfigurationSorterTests { public static class AutoConfigureW { } + @AutoConfigureBefore(name = "org.springframework.boot.autoconfigure.AutoConfigurationSorterTests$AutoConfigureB") + public static class AutoConfigureW2 { + } + public static class AutoConfigureX { }