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
pull/29976/head
Moritz Halbritter 3 years ago
parent d7b229d3c7
commit 1325153ee9

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2022 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.
@ -31,9 +31,7 @@ import org.springframework.core.annotation.Order;
/** /**
* Specialized {@link Configuration @Configuration} class that defines configuration * Specialized {@link Configuration @Configuration} class that defines configuration
* specific for the management context. Configurations should be registered in * specific for the management context. Configurations should be registered in
* {@code /META-INF/spring.factories} under the * {@code /META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports}.
* {@code org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration}
* key.
* <p> * <p>
* {@code ManagementContextConfiguration} classes can be ordered using * {@code ManagementContextConfiguration} classes can be ordered using
* {@link Order @Order}. Ordering by implementing {@link Ordered} is not supported and * {@link Order @Order}. Ordering by implementing {@link Ordered} is not supported and

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2022 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.
@ -24,6 +24,7 @@ import java.util.Map;
import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
import org.springframework.boot.context.annotation.ImportCandidates;
import org.springframework.context.annotation.DeferredImportSelector; import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.OrderComparator; import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
@ -36,14 +37,15 @@ import org.springframework.util.StringUtils;
/** /**
* Selects configuration classes for the management context configuration. Entries are * Selects configuration classes for the management context configuration. Entries are
* loaded from {@code /META-INF/spring.factories} under the * loaded from
* {@code org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration} * {@code /META-INF/spring/org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration.imports}.
* key.
* *
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Moritz Halbritter
* @see ManagementContextConfiguration * @see ManagementContextConfiguration
* @see ImportCandidates
*/ */
@Order(Ordered.LOWEST_PRECEDENCE) @Order(Ordered.LOWEST_PRECEDENCE)
class ManagementContextConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware { class ManagementContextConfigurationImportSelector implements DeferredImportSelector, BeanClassLoaderAware {
@ -88,7 +90,10 @@ class ManagementContextConfigurationImportSelector implements DeferredImportSele
} }
protected List<String> loadFactoryNames() { protected List<String> loadFactoryNames() {
return SpringFactoriesLoader.loadFactoryNames(ManagementContextConfiguration.class, this.classLoader); List<String> factoryNames = new ArrayList<>(
SpringFactoriesLoader.loadFactoryNames(ManagementContextConfiguration.class, this.classLoader));
ImportCandidates.load(ManagementContextConfiguration.class, this.classLoader).forEach(factoryNames::add);
return factoryNames;
} }
@Override @Override

@ -1,14 +1,2 @@
org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=\
org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.web.jersey.JerseyWebEndpointManagementContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.security.servlet.SecurityRequestMatchersManagementContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseySameManagementContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseyChildManagementContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration,\
org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration
org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.actuate.autoconfigure.metrics.ValidationFailureAnalyzer org.springframework.boot.actuate.autoconfigure.metrics.ValidationFailureAnalyzer

@ -0,0 +1,10 @@
org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManagementContextConfiguration
org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration
org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration
org.springframework.boot.actuate.autoconfigure.endpoint.web.jersey.JerseyWebEndpointManagementContextConfiguration
org.springframework.boot.actuate.autoconfigure.security.servlet.SecurityRequestMatchersManagementContextConfiguration
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseySameManagementContextConfiguration
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseyChildManagementContextConfiguration
org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration
org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration
org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2022 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.
@ -16,7 +16,9 @@
package org.springframework.boot.actuate.autoconfigure.web.server; package org.springframework.boot.actuate.autoconfigure.web.server;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -24,6 +26,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType; import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
import org.springframework.boot.context.annotation.ImportCandidates;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.type.AnnotationMetadata; import org.springframework.core.type.AnnotationMetadata;
@ -58,6 +61,22 @@ class ManagementContextConfigurationImportSelectorTests {
assertThat(imports).containsExactlyInAnyOrder(ChildOnly.class.getName(), A.class.getName()); assertThat(imports).containsExactlyInAnyOrder(ChildOnly.class.getName(), A.class.getName());
} }
@Test
void selectImportsLoadsFromResources() {
String[] imports = new ManagementContextConfigurationImportSelector()
.selectImports(AnnotationMetadata.introspect(EnableChildContext.class));
Set<String> expected = new HashSet<>();
ImportCandidates
.load(ManagementContextConfiguration.class,
ManagementContextConfigurationImportSelectorTests.class.getClassLoader())
.forEach(expected::add);
// Remove JerseySameManagementContextConfiguration, as it specifies
// ManagementContextType.SAME and we asked for ManagementContextType.CHILD
expected.remove(
"org.springframework.boot.actuate.autoconfigure.web.jersey.JerseySameManagementContextConfiguration");
assertThat(imports).containsExactlyInAnyOrderElementsOf(expected);
}
private static final class TestManagementContextConfigurationsImportSelector private static final class TestManagementContextConfigurationsImportSelector
extends ManagementContextConfigurationImportSelector { extends ManagementContextConfigurationImportSelector {

@ -23,13 +23,14 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.springframework.boot.context.annotation.ImportCandidates;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.annotation.AliasFor; import org.springframework.core.annotation.AliasFor;
/** /**
* Import and apply the specified auto-configuration classes. Applies the same ordering * Import and apply the specified auto-configuration classes. Applies the same ordering
* rules as {@code @EnableAutoConfiguration} but restricts the auto-configuration classes * rules as {@code @EnableAutoConfiguration} but restricts the auto-configuration classes
* to the specified set, rather than consulting {@code spring.factories}. * to the specified set, rather than consulting {@link ImportCandidates}.
* <p> * <p>
* Can also be used to {@link #exclude()} specific auto-configuration classes such that * Can also be used to {@link #exclude()} specific auto-configuration classes such that
* they will never be applied. * they will never be applied.

Loading…
Cancel
Save