|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2022 the original author or authors. |
| 2 | + * Copyright 2012-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
25 | 25 | import java.util.LinkedHashSet;
|
26 | 26 | import java.util.List;
|
27 | 27 | import java.util.Map;
|
| 28 | +import java.util.Objects; |
28 | 29 | import java.util.Set;
|
29 | 30 |
|
30 | 31 | import org.springframework.boot.context.annotation.DeterminableImports;
|
31 | 32 | import org.springframework.boot.context.annotation.ImportCandidates;
|
32 | 33 | import org.springframework.core.annotation.AnnotatedElementUtils;
|
33 | 34 | import org.springframework.core.annotation.AnnotationAttributes;
|
34 | 35 | import org.springframework.core.annotation.AnnotationUtils;
|
| 36 | +import org.springframework.core.io.ClassPathResource; |
35 | 37 | import org.springframework.core.type.AnnotationMetadata;
|
36 | 38 | import org.springframework.util.ClassUtils;
|
37 | 39 | import org.springframework.util.LinkedMultiValueMap;
|
|
49 | 51 | */
|
50 | 52 | class ImportAutoConfigurationImportSelector extends AutoConfigurationImportSelector implements DeterminableImports {
|
51 | 53 |
|
| 54 | + private static final String OPTIONAL_PREFIX = "optional:"; |
| 55 | + |
52 | 56 | private static final Set<String> ANNOTATION_NAMES;
|
53 | 57 |
|
54 | 58 | static {
|
@@ -92,13 +96,27 @@ private Collection<String> getConfigurationsForAnnotation(Class<?> source, Annot
|
92 | 96 | if (classes.length > 0) {
|
93 | 97 | return Arrays.asList(classes);
|
94 | 98 | }
|
95 |
| - return loadFactoryNames(source); |
| 99 | + Collection<String> factoryNames = loadFactoryNames(source); |
| 100 | + return factoryNames.stream().map((name) -> { |
| 101 | + if (name.startsWith(OPTIONAL_PREFIX)) { |
| 102 | + name = name.substring(OPTIONAL_PREFIX.length()); |
| 103 | + if (!present(name)) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + } |
| 107 | + return name; |
| 108 | + }).filter(Objects::nonNull).toList(); |
96 | 109 | }
|
97 | 110 |
|
98 | 111 | protected Collection<String> loadFactoryNames(Class<?> source) {
|
99 | 112 | return ImportCandidates.load(source, getBeanClassLoader()).getCandidates();
|
100 | 113 | }
|
101 | 114 |
|
| 115 | + private boolean present(String className) { |
| 116 | + String resourcePath = ClassUtils.convertClassNameToResourcePath(className) + ".class"; |
| 117 | + return new ClassPathResource(resourcePath).exists(); |
| 118 | + } |
| 119 | + |
102 | 120 | @Override
|
103 | 121 | protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttributes attributes) {
|
104 | 122 | Set<String> exclusions = new LinkedHashSet<>();
|
|
0 commit comments