Skip to content

Commit 7320119

Browse files
committed
Fix ManagementContextConfiguration @order support
Update `ManagementContextConfigurationsImportSelector` to instantiate classes so that order annotations are respected. Fixes spring-projectsgh-7814
1 parent 556ce14 commit 7320119

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementContextConfigurationsImportSelector.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.LinkedHashSet;
2121
import java.util.List;
22+
import java.util.Set;
2223

2324
import org.springframework.beans.factory.BeanClassLoaderAware;
2425
import org.springframework.context.annotation.DeferredImportSelector;
@@ -27,6 +28,7 @@
2728
import org.springframework.core.annotation.Order;
2829
import org.springframework.core.io.support.SpringFactoriesLoader;
2930
import org.springframework.core.type.AnnotationMetadata;
31+
import org.springframework.util.ClassUtils;
3032

3133
/**
3234
* Selects configuration classes for the management context configuration. Entries are
@@ -47,11 +49,26 @@ class ManagementContextConfigurationsImportSelector
4749
@Override
4850
public String[] selectImports(AnnotationMetadata metadata) {
4951
// Find all possible auto configuration classes, filtering duplicates
50-
List<String> factories = new ArrayList<String>(
51-
new LinkedHashSet<String>(SpringFactoriesLoader.loadFactoryNames(
52-
ManagementContextConfiguration.class, this.classLoader)));
53-
AnnotationAwareOrderComparator.sort(factories);
54-
return factories.toArray(new String[0]);
52+
List<String> names = loadFactoryNames();
53+
Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
54+
for (String factoryName : names) {
55+
classes.add(ClassUtils.resolveClassName(factoryName, this.classLoader));
56+
}
57+
return getSortedClassNames(new ArrayList<Class<?>>(classes));
58+
}
59+
60+
protected List<String> loadFactoryNames() {
61+
return SpringFactoriesLoader
62+
.loadFactoryNames(ManagementContextConfiguration.class, this.classLoader);
63+
}
64+
65+
private String[] getSortedClassNames(List<Class<?>> classes) {
66+
AnnotationAwareOrderComparator.sort(classes);
67+
List<String> names = new ArrayList<String>();
68+
for (Class<?> sourceClass : classes) {
69+
names.add(sourceClass.getName());
70+
}
71+
return names.toArray(new String[names.size()]);
5572
}
5673

5774
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure;
18+
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
import org.junit.Test;
23+
24+
import org.springframework.core.annotation.Order;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link ManagementContextConfigurationsImportSelector}.
30+
*
31+
* @author Phillip Webb
32+
*/
33+
public class ManagementContextConfigurationsImportSelectorTests {
34+
35+
@Test
36+
public void selectImportsShouldOrderResult() throws Exception {
37+
String[] imports = new TestManagementContextConfigurationsImportSelector()
38+
.selectImports(null);
39+
assertThat(imports).containsExactly(A.class.getName(), B.class.getName(),
40+
C.class.getName());
41+
}
42+
43+
private static class TestManagementContextConfigurationsImportSelector
44+
extends ManagementContextConfigurationsImportSelector {
45+
46+
@Override
47+
protected List<String> loadFactoryNames() {
48+
return Arrays.asList(C.class.getName(), A.class.getName(), B.class.getName());
49+
}
50+
51+
}
52+
53+
@Order(1)
54+
private static class A {
55+
56+
}
57+
58+
@Order(2)
59+
private static class B {
60+
61+
}
62+
63+
@Order(3)
64+
private static class C {
65+
66+
}
67+
68+
}

0 commit comments

Comments
 (0)