pull/30016/head
Phillip Webb 3 years ago
parent adfaa8f778
commit 9f00c3acf5

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 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.

@ -117,7 +117,6 @@ public class TestSliceMetadata extends DefaultTask {
/** /**
* Reads files from the given directory and puts them in springFactories. The key is * Reads files from the given directory and puts them in springFactories. The key is
* the file name, the value is the file contents, split by line, delimited with comma. * the file name, the value is the file contents, split by line, delimited with comma.
*
* This is done to mimic the spring.factories structure. * This is done to mimic the spring.factories structure.
* @param springFactories spring.factories parsed as properties * @param springFactories spring.factories parsed as properties
* @param directory directory to scan * @param directory directory to scan

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 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.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 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.

@ -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.

@ -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.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 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.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 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.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 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.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 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.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 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.

@ -44,6 +44,8 @@ Specific ``@Import``s should be used instead.
You can use the {spring-boot-autoconfigure-module-code}/AutoConfigureAfter.java[`@AutoConfigureAfter`] or {spring-boot-autoconfigure-module-code}/AutoConfigureBefore.java[`@AutoConfigureBefore`] annotations if your configuration needs to be applied in a specific order. You can use the {spring-boot-autoconfigure-module-code}/AutoConfigureAfter.java[`@AutoConfigureAfter`] or {spring-boot-autoconfigure-module-code}/AutoConfigureBefore.java[`@AutoConfigureBefore`] annotations if your configuration needs to be applied in a specific order.
For example, if you provide web-specific configuration, your class may need to be applied after `WebMvcAutoConfiguration`. For example, if you provide web-specific configuration, your class may need to be applied after `WebMvcAutoConfiguration`.
TIP: If you are using the {spring-boot-autoconfigure-module-code}/AutoConfiguration.java[`@AutoConfiguration`] annotation, you can use the `before`, `beforeName`, `after` and `afterName` attribute aliases instead of the dedicated annotations.
If you want to order certain auto-configurations that should not have any direct knowledge of each other, you can also use `@AutoConfigureOrder`. If you want to order certain auto-configurations that should not have any direct knowledge of each other, you can also use `@AutoConfigureOrder`.
That annotation has the same semantic as the regular `@Order` annotation but provides a dedicated order for auto-configuration classes. That annotation has the same semantic as the regular `@Order` annotation but provides a dedicated order for auto-configuration classes.

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 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.

@ -76,31 +76,35 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
protected List<PropertyGenerator> getPropertyGenerators() { protected List<PropertyGenerator> getPropertyGenerators() {
List<PropertyGenerator> generators = new ArrayList<>(); List<PropertyGenerator> generators = new ArrayList<>();
generators.add(PropertyGenerator.of("ConditionalOnClass", addConditionPropertyGenerators(generators);
"org.springframework.boot.autoconfigure.condition.ConditionalOnClass", addAutoConfigurePropertyGenerators(generators);
new OnClassConditionValueExtractor()));
generators.add(PropertyGenerator.of("ConditionalOnBean",
"org.springframework.boot.autoconfigure.condition.ConditionalOnBean",
new OnBeanConditionValueExtractor()));
generators.add(PropertyGenerator.of("ConditionalOnSingleCandidate",
"org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate",
new OnBeanConditionValueExtractor()));
generators.add(PropertyGenerator.of("ConditionalOnWebApplication",
"org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication",
ValueExtractor.allFrom("type")));
generators.add(PropertyGenerator.of("AutoConfigureBefore", true,
"org.springframework.boot.autoconfigure.AutoConfigureBefore", ValueExtractor.allFrom("value", "name"),
"org.springframework.boot.autoconfigure.AutoConfiguration",
ValueExtractor.allFrom("before", "beforeName")));
generators.add(PropertyGenerator.of("AutoConfigureAfter", true,
"org.springframework.boot.autoconfigure.AutoConfigureAfter", ValueExtractor.allFrom("value", "name"),
"org.springframework.boot.autoconfigure.AutoConfiguration",
ValueExtractor.allFrom("after", "afterName")));
generators.add(PropertyGenerator.of("AutoConfigureOrder",
"org.springframework.boot.autoconfigure.AutoConfigureOrder", ValueExtractor.allFrom("value")));
return generators; return generators;
} }
private void addConditionPropertyGenerators(List<PropertyGenerator> generators) {
String annotationPackage = "org.springframework.boot.autoconfigure.condition";
generators.add(PropertyGenerator.of(annotationPackage, "ConditionalOnClass")
.withAnnotation(new OnClassConditionValueExtractor()));
generators.add(PropertyGenerator.of(annotationPackage, "ConditionalOnBean")
.withAnnotation(new OnBeanConditionValueExtractor()));
generators.add(PropertyGenerator.of(annotationPackage, "ConditionalOnSingleCandidate")
.withAnnotation(new OnBeanConditionValueExtractor()));
generators.add(PropertyGenerator.of(annotationPackage, "ConditionalOnWebApplication")
.withAnnotation(ValueExtractor.allFrom("type")));
}
private void addAutoConfigurePropertyGenerators(List<PropertyGenerator> generators) {
String annotationPackage = "org.springframework.boot.autoconfigure";
generators.add(PropertyGenerator.of(annotationPackage, "AutoConfigureBefore", true)
.withAnnotation(ValueExtractor.allFrom("value", "name"))
.withAnnotation("AutoConfiguration", ValueExtractor.allFrom("before", "beforeName")));
generators.add(PropertyGenerator.of(annotationPackage, "AutoConfigureAfter", true)
.withAnnotation(ValueExtractor.allFrom("value", "name"))
.withAnnotation("AutoConfiguration", ValueExtractor.allFrom("after", "afterName")));
generators.add(PropertyGenerator.of(annotationPackage, "AutoConfigureOrder")
.withAnnotation(ValueExtractor.allFrom("value")));
}
@Override @Override
public SourceVersion getSupportedSourceVersion() { public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported(); return SourceVersion.latestSupported();
@ -282,24 +286,35 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
static final class PropertyGenerator { static final class PropertyGenerator {
private final String keyName; private final String annotationPackage;
private final String propertyName;
private final boolean omitEmptyValues; private final boolean omitEmptyValues;
/**
* Maps from annotation class name -> {@link ValueExtractor}.
*/
private final Map<String, ValueExtractor> valueExtractors; private final Map<String, ValueExtractor> valueExtractors;
private PropertyGenerator(String keyName, boolean omitEmptyValues, private PropertyGenerator(String annotationPackage, String propertyName, boolean omitEmptyValues,
Map<String, ValueExtractor> valueExtractors) { Map<String, ValueExtractor> valueExtractors) {
this.keyName = keyName; this.annotationPackage = annotationPackage;
this.propertyName = propertyName;
this.omitEmptyValues = omitEmptyValues; this.omitEmptyValues = omitEmptyValues;
this.valueExtractors = valueExtractors; this.valueExtractors = valueExtractors;
} }
PropertyGenerator withAnnotation(ValueExtractor valueExtractor) {
return withAnnotation(this.propertyName, valueExtractor);
}
PropertyGenerator withAnnotation(String name, ValueExtractor ValueExtractor) {
Map<String, ValueExtractor> valueExtractors = new LinkedHashMap<>(this.valueExtractors);
valueExtractors.put(this.annotationPackage + "." + name, ValueExtractor);
return new PropertyGenerator(this.annotationPackage, this.propertyName, this.omitEmptyValues,
valueExtractors);
}
Set<String> getSupportedAnnotations() { Set<String> getSupportedAnnotations() {
return Collections.unmodifiableSet(this.valueExtractors.keySet()); return this.valueExtractors.keySet();
} }
ValueExtractor getValueExtractor(String annotation) { ValueExtractor getValueExtractor(String annotation) {
@ -310,7 +325,7 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
if (this.omitEmptyValues && annotationValues.isEmpty()) { if (this.omitEmptyValues && annotationValues.isEmpty()) {
return; return;
} }
mergeProperties(properties, className + "." + this.keyName, toCommaDelimitedString(annotationValues)); mergeProperties(properties, className + "." + this.propertyName, toCommaDelimitedString(annotationValues));
} }
private void mergeProperties(Map<String, String> properties, String key, String value) { private void mergeProperties(Map<String, String> properties, String key, String value) {
@ -335,27 +350,12 @@ public class AutoConfigureAnnotationProcessor extends AbstractProcessor {
return result.toString(); return result.toString();
} }
static PropertyGenerator of(String keyName, String annotation, ValueExtractor valueExtractor) { static PropertyGenerator of(String annotationPackage, String propertyName) {
return of(keyName, false, annotation, valueExtractor); return of(annotationPackage, propertyName, false);
}
static PropertyGenerator of(String keyName, boolean omitEmptyValues, String annotation,
ValueExtractor valueExtractor) {
return new PropertyGenerator(keyName, omitEmptyValues,
Collections.singletonMap(annotation, valueExtractor));
}
static PropertyGenerator of(String keyName, String annotation1, ValueExtractor valueExtractor1,
String annotation2, ValueExtractor valueExtractor2) {
return of(keyName, false, annotation1, valueExtractor1, annotation2, valueExtractor2);
} }
static PropertyGenerator of(String keyName, boolean omitEmptyValues, String annotation1, static PropertyGenerator of(String annotationPackage, String propertyName, boolean omitEmptyValues) {
ValueExtractor valueExtractor1, String annotation2, ValueExtractor valueExtractor2) { return new PropertyGenerator(annotationPackage, propertyName, omitEmptyValues, Collections.emptyMap());
Map<String, ValueExtractor> valueExtractors = new LinkedHashMap<>();
valueExtractors.put(annotation1, valueExtractor1);
valueExtractors.put(annotation2, valueExtractor2);
return new PropertyGenerator(keyName, omitEmptyValues, valueExtractors);
} }
} }

@ -49,22 +49,23 @@ public class TestAutoConfigureAnnotationProcessor extends AutoConfigureAnnotatio
@Override @Override
protected List<PropertyGenerator> getPropertyGenerators() { protected List<PropertyGenerator> getPropertyGenerators() {
List<PropertyGenerator> generators = new ArrayList<>(); List<PropertyGenerator> generators = new ArrayList<>();
generators.add(PropertyGenerator.of("ConditionalOnClass", TestConditionalOnClass.class.getName(), String annotationPackage = "org.springframework.boot.autoconfigureprocessor";
new OnClassConditionValueExtractor())); generators.add(PropertyGenerator.of(annotationPackage, "ConditionalOnClass")
generators.add(PropertyGenerator.of("ConditionalOnBean", TestConditionalOnBean.class.getName(), .withAnnotation("TestConditionalOnClass", new OnClassConditionValueExtractor()));
new OnBeanConditionValueExtractor())); generators.add(PropertyGenerator.of(annotationPackage, "ConditionalOnBean")
generators.add(PropertyGenerator.of("ConditionalOnSingleCandidate", .withAnnotation("TestConditionalOnBean", new OnBeanConditionValueExtractor()));
TestConditionalOnSingleCandidate.class.getName(), new OnBeanConditionValueExtractor())); generators.add(PropertyGenerator.of(annotationPackage, "ConditionalOnSingleCandidate")
generators.add(PropertyGenerator.of("ConditionalOnWebApplication", .withAnnotation("TestConditionalOnSingleCandidate", new OnBeanConditionValueExtractor()));
TestConditionalOnWebApplication.class.getName(), ValueExtractor.allFrom("type"))); generators.add(PropertyGenerator.of(annotationPackage, "ConditionalOnWebApplication")
generators.add(PropertyGenerator.of("AutoConfigureBefore", true, TestAutoConfigureBefore.class.getName(), .withAnnotation("TestConditionalOnWebApplication", ValueExtractor.allFrom("type")));
ValueExtractor.allFrom("value", "name"), TestAutoConfiguration.class.getName(), generators.add(PropertyGenerator.of(annotationPackage, "AutoConfigureBefore", true)
ValueExtractor.allFrom("before", "beforeName"))); .withAnnotation("TestAutoConfigureBefore", ValueExtractor.allFrom("value", "name"))
generators.add(PropertyGenerator.of("AutoConfigureAfter", true, TestAutoConfigureAfter.class.getName(), .withAnnotation("TestAutoConfiguration", ValueExtractor.allFrom("before", "beforeName")));
ValueExtractor.allFrom("value", "name"), TestAutoConfiguration.class.getName(), generators.add(PropertyGenerator.of(annotationPackage, "AutoConfigureAfter", true)
ValueExtractor.allFrom("after", "afterName"))); .withAnnotation("TestAutoConfigureAfter", ValueExtractor.allFrom("value", "name"))
generators.add(PropertyGenerator.of("AutoConfigureOrder", TestAutoConfigureOrder.class.getName(), .withAnnotation("TestAutoConfiguration", ValueExtractor.allFrom("after", "afterName")));
ValueExtractor.allFrom("value"))); generators.add(PropertyGenerator.of(annotationPackage, "AutoConfigureOrder")
.withAnnotation("TestAutoConfigureOrder", ValueExtractor.allFrom("value")));
return generators; return generators;
} }

@ -33,7 +33,7 @@ import org.springframework.core.io.UrlResource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
/** /**
* Contains import candidates, usually auto-configurations. * Contains {@code @Configuration} import candidates, usually auto-configurations.
* *
* The {@link #load(Class, ClassLoader)} method can be used to discover the import * The {@link #load(Class, ClassLoader)} method can be used to discover the import
* candidates. * candidates.

Loading…
Cancel
Save