Skip to content

Commit 1325153

Browse files
committed
Move ManagementContextConfiguration from spring.factories
The import selector will now, in addition to spring.factories, look for ManagementContextConfiguration classes in a file called META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports The existing ManagementContextConfigurations have been moved from spring.factories to the new file. Closes gh-29730
1 parent d7b229d commit 1325153

File tree

6 files changed

+44
-23
lines changed

6 files changed

+44
-23
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/ManagementContextConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -31,9 +31,7 @@
3131
/**
3232
* Specialized {@link Configuration @Configuration} class that defines configuration
3333
* specific for the management context. Configurations should be registered in
34-
* {@code /META-INF/spring.factories} under the
35-
* {@code org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration}
36-
* key.
34+
* {@code /META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports}.
3735
* <p>
3836
* {@code ManagementContextConfiguration} classes can be ordered using
3937
* {@link Order @Order}. Ordering by implementing {@link Ordered} is not supported and

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelector.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -24,6 +24,7 @@
2424
import org.springframework.beans.factory.BeanClassLoaderAware;
2525
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
2626
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
27+
import org.springframework.boot.context.annotation.ImportCandidates;
2728
import org.springframework.context.annotation.DeferredImportSelector;
2829
import org.springframework.core.OrderComparator;
2930
import org.springframework.core.Ordered;
@@ -36,14 +37,15 @@
3637

3738
/**
3839
* Selects configuration classes for the management context configuration. Entries are
39-
* loaded from {@code /META-INF/spring.factories} under the
40-
* {@code org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration}
41-
* key.
40+
* loaded from
41+
* {@code /META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports}.
4242
*
4343
* @author Dave Syer
4444
* @author Phillip Webb
4545
* @author Andy Wilkinson
46+
* @author Moritz Halbritter
4647
* @see ManagementContextConfiguration
48+
* @see ImportCandidates
4749
*/
4850
@Order(Ordered.LOWEST_PRECEDENCE)
4951
class ManagementContextConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware {
@@ -88,7 +90,10 @@ private void addConfiguration(SimpleMetadataReaderFactory readerFactory,
8890
}
8991

9092
protected List<String> loadFactoryNames() {
91-
return SpringFactoriesLoader.loadFactoryNames(ManagementContextConfiguration.class, this.classLoader);
93+
List<String> factoryNames = new ArrayList<>(
94+
SpringFactoriesLoader.loadFactoryNames(ManagementContextConfiguration.class, this.classLoader));
95+
ImportCandidates.load(ManagementContextConfiguration.class, this.classLoader).forEach(factoryNames::add);
96+
return factoryNames;
9297
}
9398

9499
@Override
Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,2 @@
1-
org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=\
2-
org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration,\
3-
org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration,\
4-
org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration,\
5-
org.springframework.boot.actuate.autoconfigure.endpoint.web.jersey.JerseyWebEndpointManagementContextConfiguration,\
6-
org.springframework.boot.actuate.autoconfigure.security.servlet.SecurityRequestMatchersManagementContextConfiguration,\
7-
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseySameManagementContextConfiguration,\
8-
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseyChildManagementContextConfiguration,\
9-
org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration,\
10-
org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration,\
11-
org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration
12-
131
org.springframework.boot.diagnostics.FailureAnalyzer=\
142
org.springframework.boot.actuate.autoconfigure.metrics.ValidationFailureAnalyzer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration
2+
org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration
3+
org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration
4+
org.springframework.boot.actuate.autoconfigure.endpoint.web.jersey.JerseyWebEndpointManagementContextConfiguration
5+
org.springframework.boot.actuate.autoconfigure.security.servlet.SecurityRequestMatchersManagementContextConfiguration
6+
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseySameManagementContextConfiguration
7+
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseyChildManagementContextConfiguration
8+
org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration
9+
org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration
10+
org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextConfigurationImportSelectorTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -16,14 +16,17 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.web.server;
1818

19+
import java.util.HashSet;
1920
import java.util.List;
21+
import java.util.Set;
2022
import java.util.stream.Collectors;
2123
import java.util.stream.Stream;
2224

2325
import org.junit.jupiter.api.Test;
2426

2527
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
2628
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
29+
import org.springframework.boot.context.annotation.ImportCandidates;
2730
import org.springframework.core.annotation.Order;
2831
import org.springframework.core.type.AnnotationMetadata;
2932

@@ -58,6 +61,22 @@ void selectImportsFiltersSameOnlyConfigurationWhenUsingChildContext() {
5861
assertThat(imports).containsExactlyInAnyOrder(ChildOnly.class.getName(), A.class.getName());
5962
}
6063

64+
@Test
65+
void selectImportsLoadsFromResources() {
66+
String[] imports = new ManagementContextConfigurationImportSelector()
67+
.selectImports(AnnotationMetadata.introspect(EnableChildContext.class));
68+
Set<String> expected = new HashSet<>();
69+
ImportCandidates
70+
.load(ManagementContextConfiguration.class,
71+
ManagementContextConfigurationImportSelectorTests.class.getClassLoader())
72+
.forEach(expected::add);
73+
// Remove JerseySameManagementContextConfiguration, as it specifies
74+
// ManagementContextType.SAME and we asked for ManagementContextType.CHILD
75+
expected.remove(
76+
"org.springframework.boot.actuate.autoconfigure.web.jersey.JerseySameManagementContextConfiguration");
77+
assertThat(imports).containsExactlyInAnyOrderElementsOf(expected);
78+
}
79+
6180
private static final class TestManagementContextConfigurationsImportSelector
6281
extends ManagementContextConfigurationImportSelector {
6382

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ImportAutoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
import java.lang.annotation.RetentionPolicy;
2424
import java.lang.annotation.Target;
2525

26+
import org.springframework.boot.context.annotation.ImportCandidates;
2627
import org.springframework.context.annotation.Import;
2728
import org.springframework.core.annotation.AliasFor;
2829

2930
/**
3031
* Import and apply the specified auto-configuration classes. Applies the same ordering
3132
* rules as {@code @EnableAutoConfiguration} but restricts the auto-configuration classes
32-
* to the specified set, rather than consulting {@code spring.factories}.
33+
* to the specified set, rather than consulting {@link ImportCandidates}.
3334
* <p>
3435
* Can also be used to {@link #exclude()} specific auto-configuration classes such that
3536
* they will never be applied.

0 commit comments

Comments
 (0)