20
20
import java .util .LinkedHashSet ;
21
21
import java .util .List ;
22
22
23
- import lombok .extern .apachecommons .CommonsLog ;
24
-
25
23
import org .springframework .beans .factory .BeanClassLoaderAware ;
26
24
import org .springframework .context .EnvironmentAware ;
27
25
import org .springframework .context .annotation .DeferredImportSelector ;
32
30
import org .springframework .core .type .AnnotationMetadata ;
33
31
import org .springframework .util .Assert ;
34
32
33
+ import lombok .extern .apachecommons .CommonsLog ;
34
+
35
35
/**
36
- * Selects configurations to load defined by the generic type T. Loads
37
- * implementations using {@link SpringFactoriesLoader}.
36
+ * Selects configurations to load defined by the generic type T. Loads implementations
37
+ * using {@link SpringFactoriesLoader}.
38
38
*
39
39
* @author Spencer Gibb
40
40
* @author Dave Syer
41
41
*/
42
42
@ CommonsLog
43
- public abstract class SpringFactoryImportSelector <T > implements
44
- DeferredImportSelector , BeanClassLoaderAware , EnvironmentAware {
43
+ public abstract class SpringFactoryImportSelector <T >
44
+ implements DeferredImportSelector , BeanClassLoaderAware , EnvironmentAware {
45
45
46
46
private ClassLoader beanClassLoader ;
47
47
@@ -51,40 +51,44 @@ public abstract class SpringFactoryImportSelector<T> implements
51
51
52
52
@ SuppressWarnings ("unchecked" )
53
53
protected SpringFactoryImportSelector () {
54
- this .annotationClass = (Class <T >) GenericTypeResolver . resolveTypeArgument (
55
- this .getClass (), SpringFactoryImportSelector .class );
54
+ this .annotationClass = (Class <T >) GenericTypeResolver
55
+ . resolveTypeArgument ( this .getClass (), SpringFactoryImportSelector .class );
56
56
}
57
57
58
58
@ Override
59
59
public String [] selectImports (AnnotationMetadata metadata ) {
60
60
if (!isEnabled ()) {
61
61
return new String [0 ];
62
62
}
63
- AnnotationAttributes attributes = AnnotationAttributes .fromMap (metadata
64
- .getAnnotationAttributes (this .annotationClass .getName (), true ));
63
+ AnnotationAttributes attributes = AnnotationAttributes .fromMap (
64
+ metadata .getAnnotationAttributes (this .annotationClass .getName (), true ));
65
65
66
66
Assert .notNull (attributes , "No " + getSimpleName () + " attributes found. Is "
67
67
+ metadata .getClassName () + " annotated with @" + getSimpleName () + "?" );
68
68
69
69
// Find all possible auto configuration classes, filtering duplicates
70
- List <String > factories = new ArrayList <>(new LinkedHashSet <>(
71
- SpringFactoriesLoader .loadFactoryNames (this .annotationClass ,
72
- this .beanClassLoader )));
70
+ List <String > factories = new ArrayList <>(new LinkedHashSet <>(SpringFactoriesLoader
71
+ .loadFactoryNames (this .annotationClass , this .beanClassLoader )));
73
72
74
- if (factories .isEmpty ()) {
75
- throw new IllegalStateException ("Annotation @" + getSimpleName () +
76
- " found, but there are no implementations. Did you forget to include a starter?" );
73
+ if (factories .isEmpty () && ! hasDefaultFactory () ) {
74
+ throw new IllegalStateException ("Annotation @" + getSimpleName ()
75
+ + " found, but there are no implementations. Did you forget to include a starter?" );
77
76
}
78
77
79
78
if (factories .size () > 1 ) {
80
- // there should only ever be one DiscoveryClient, but there might be more than one factory
79
+ // there should only ever be one DiscoveryClient, but there might be more than
80
+ // one factory
81
81
log .warn ("More than one implementation " + "of @" + getSimpleName ()
82
82
+ " (now relying on @Conditionals to pick one): " + factories );
83
83
}
84
84
85
85
return factories .toArray (new String [factories .size ()]);
86
86
}
87
87
88
+ protected boolean hasDefaultFactory () {
89
+ return false ;
90
+ }
91
+
88
92
protected abstract boolean isEnabled ();
89
93
90
94
protected String getSimpleName () {
0 commit comments