mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Support string names @AutoConfigureBefore/After
Update @AutoConfigureBefore and @AutoConfigureAfter annotations to support String classnames in addition direct Class references. Fixes gh-2529
This commit is contained in:
parent
affa584916
commit
5c4b698f86
@ -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<String> getInPriorityOrder(Collection<String> classNames)
|
||||
throws IOException {
|
||||
|
||||
final AutoConfigurationClasses classes = new AutoConfigurationClasses(
|
||||
this.metadataReaderFactory, classNames);
|
||||
|
||||
List<String> orderedClassNames = new ArrayList<String>(classNames);
|
||||
|
||||
// Initially sort alphabetically
|
||||
Collections.sort(orderedClassNames);
|
||||
|
||||
// Then sort by order
|
||||
Collections.sort(orderedClassNames, new Comparator<String>() {
|
||||
@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<String> sortByAnnotation(AutoConfigurationClasses classes,
|
||||
@ -170,7 +161,10 @@ class AutoConfigurationSorter {
|
||||
if (attributes == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return new HashSet<String>(Arrays.asList((String[]) attributes.get("value")));
|
||||
Set<String> value = new LinkedHashSet<String>();
|
||||
Collections.addAll(value, (String[]) attributes.get("value"));
|
||||
Collections.addAll(value, (String[]) attributes.get("name"));
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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 {};
|
||||
|
||||
}
|
||||
|
@ -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 {};
|
||||
|
||||
}
|
||||
|
@ -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 {};
|
||||
|
||||
}
|
||||
|
@ -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<String> 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 {
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user