Order ManagementContextConfiguration classes without loading them
Previously, ManagementContextConfiguration classes were loaded to allow them to be ordered based on either @Order or implementing Ordered. This had the unwanted side-effect of possibly logging unwanted INFO messages if the reflection-based annotation introspection failed. One cause of this was @ConditionalOnClass when the referenced class was not on the classpath. This commit uses the ASM-based annotation metadata reading to determine the order of a management context configuration class based on the @Order annotation. The classes are then sorted using a standard OrderComparator. Note that Ordering via implemented Ordered is not supported as it cannot be determine without loading the class.
This commit is contained in:
parent
bd0c1cb9c3
commit
f3b9b14b8e
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2015 the original author or authors.
|
* Copyright 2012-2017 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.
|
||||||
|
|
@ -23,6 +23,8 @@ import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
import org.springframework.core.annotation.Order;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specialized {@link Configuration @Configuration} class that defines configuration
|
* Specialized {@link Configuration @Configuration} class that defines configuration
|
||||||
|
|
@ -30,8 +32,12 @@ import org.springframework.context.annotation.Configuration;
|
||||||
* {@code /META-INF/spring.factories} under the
|
* {@code /META-INF/spring.factories} under the
|
||||||
* {@code org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration}
|
* {@code org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration}
|
||||||
* key.
|
* key.
|
||||||
|
* <p>
|
||||||
|
* {@code ManagementContextConfiguration} classes can be ordered using {@link Order}.
|
||||||
|
* Ordering by implementing {@link Ordered} is not supported and will have no effect.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Andy Wilkinson
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
*/
|
*/
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
|
|
|
||||||
|
|
@ -16,19 +16,20 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.autoconfigure;
|
package org.springframework.boot.actuate.autoconfigure;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedHashSet;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||||
import org.springframework.context.annotation.DeferredImportSelector;
|
import org.springframework.context.annotation.DeferredImportSelector;
|
||||||
|
import org.springframework.core.OrderComparator;
|
||||||
import org.springframework.core.Ordered;
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.core.type.classreading.MetadataReader;
|
||||||
|
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selects configuration classes for the management context configuration. Entries are
|
* Selects configuration classes for the management context configuration. Entries are
|
||||||
|
|
@ -38,6 +39,7 @@ import org.springframework.util.ClassUtils;
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Andy Wilkinson
|
||||||
* @see ManagementContextConfiguration
|
* @see ManagementContextConfiguration
|
||||||
*/
|
*/
|
||||||
@Order(Ordered.LOWEST_PRECEDENCE)
|
@Order(Ordered.LOWEST_PRECEDENCE)
|
||||||
|
|
@ -48,13 +50,36 @@ class ManagementContextConfigurationsImportSelector
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] selectImports(AnnotationMetadata metadata) {
|
public String[] selectImports(AnnotationMetadata metadata) {
|
||||||
// Find all possible auto configuration classes, filtering duplicates
|
// Find all management context configuration classes, filtering duplicates
|
||||||
List<String> names = loadFactoryNames();
|
List<ManagementConfiguration> configurations = getConfigurations();
|
||||||
Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
|
OrderComparator.sort(configurations);
|
||||||
for (String factoryName : names) {
|
List<String> names = new ArrayList<String>();
|
||||||
classes.add(ClassUtils.resolveClassName(factoryName, this.classLoader));
|
for (ManagementConfiguration configuration : configurations) {
|
||||||
|
names.add(configuration.getClassName());
|
||||||
|
}
|
||||||
|
return names.toArray(new String[names.size()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<ManagementConfiguration> getConfigurations() {
|
||||||
|
SimpleMetadataReaderFactory readerFactory = new SimpleMetadataReaderFactory(
|
||||||
|
this.classLoader);
|
||||||
|
List<ManagementConfiguration> configurations = new ArrayList<ManagementConfiguration>();
|
||||||
|
for (String className : loadFactoryNames()) {
|
||||||
|
getConfiguration(readerFactory, configurations, className);
|
||||||
|
}
|
||||||
|
return configurations;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void getConfiguration(SimpleMetadataReaderFactory readerFactory,
|
||||||
|
List<ManagementConfiguration> configurations, String className) {
|
||||||
|
try {
|
||||||
|
MetadataReader metadataReader = readerFactory.getMetadataReader(className);
|
||||||
|
configurations.add(new ManagementConfiguration(metadataReader));
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
|
throw new RuntimeException(
|
||||||
|
"Failed to read annotation metadata for '" + className + "'", ex);
|
||||||
}
|
}
|
||||||
return getSortedClassNames(new ArrayList<Class<?>>(classes));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<String> loadFactoryNames() {
|
protected List<String> loadFactoryNames() {
|
||||||
|
|
@ -62,18 +87,44 @@ class ManagementContextConfigurationsImportSelector
|
||||||
.loadFactoryNames(ManagementContextConfiguration.class, this.classLoader);
|
.loadFactoryNames(ManagementContextConfiguration.class, this.classLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String[] getSortedClassNames(List<Class<?>> classes) {
|
|
||||||
AnnotationAwareOrderComparator.sort(classes);
|
|
||||||
List<String> names = new ArrayList<String>();
|
|
||||||
for (Class<?> sourceClass : classes) {
|
|
||||||
names.add(sourceClass.getName());
|
|
||||||
}
|
|
||||||
return names.toArray(new String[names.size()]);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||||
this.classLoader = classLoader;
|
this.classLoader = classLoader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A management configuration class which can be sorted according to {@code @Order}.
|
||||||
|
*/
|
||||||
|
private static final class ManagementConfiguration implements Ordered {
|
||||||
|
|
||||||
|
private final String className;
|
||||||
|
|
||||||
|
private final int order;
|
||||||
|
|
||||||
|
ManagementConfiguration(MetadataReader metadataReader) {
|
||||||
|
AnnotationMetadata annotationMetadata = metadataReader
|
||||||
|
.getAnnotationMetadata();
|
||||||
|
this.order = readOrder(annotationMetadata);
|
||||||
|
this.className = metadataReader.getClassMetadata().getClassName();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int readOrder(AnnotationMetadata annotationMetadata) {
|
||||||
|
Map<String, Object> attributes = annotationMetadata
|
||||||
|
.getAnnotationAttributes(Order.class.getName());
|
||||||
|
Integer order = (attributes == null ? null
|
||||||
|
: (Integer) attributes.get("value"));
|
||||||
|
return (order == null ? Ordered.LOWEST_PRECEDENCE : order);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClassName() {
|
||||||
|
return this.className;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return this.order;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* Tests for {@link ManagementContextConfigurationsImportSelector}.
|
* Tests for {@link ManagementContextConfigurationsImportSelector}.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Andy Wilkinson
|
||||||
*/
|
*/
|
||||||
public class ManagementContextConfigurationsImportSelectorTests {
|
public class ManagementContextConfigurationsImportSelectorTests {
|
||||||
|
|
||||||
|
|
@ -37,7 +38,7 @@ public class ManagementContextConfigurationsImportSelectorTests {
|
||||||
String[] imports = new TestManagementContextConfigurationsImportSelector()
|
String[] imports = new TestManagementContextConfigurationsImportSelector()
|
||||||
.selectImports(null);
|
.selectImports(null);
|
||||||
assertThat(imports).containsExactly(A.class.getName(), B.class.getName(),
|
assertThat(imports).containsExactly(A.class.getName(), B.class.getName(),
|
||||||
C.class.getName());
|
C.class.getName(), D.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TestManagementContextConfigurationsImportSelector
|
private static class TestManagementContextConfigurationsImportSelector
|
||||||
|
|
@ -45,7 +46,8 @@ public class ManagementContextConfigurationsImportSelectorTests {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<String> loadFactoryNames() {
|
protected List<String> loadFactoryNames() {
|
||||||
return Arrays.asList(C.class.getName(), A.class.getName(), B.class.getName());
|
return Arrays.asList(C.class.getName(), A.class.getName(), D.class.getName(),
|
||||||
|
B.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -65,4 +67,8 @@ public class ManagementContextConfigurationsImportSelectorTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class D {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue