41
41
import org .springframework .core .env .StandardEnvironment ;
42
42
import org .springframework .core .io .Resource ;
43
43
import org .springframework .core .io .ResourceLoader ;
44
- import org .springframework .core .io .support .PathMatchingResourcePatternResolver ;
45
44
import org .springframework .core .io .support .ResourcePatternResolver ;
46
45
import org .springframework .core .io .support .ResourcePatternUtils ;
47
46
import org .springframework .core .type .classreading .CachingMetadataReaderFactory ;
63
62
* use {@link CandidateComponentsIndex the index} if it is available of scans the
64
63
* classpath otherwise. Candidate components are identified by applying exclude and
65
64
* include filters. {@link AnnotationTypeFilter}, {@link AssignableTypeFilter} include
66
- * filters on an annotation/super-class that are annotated with {@link Indexed} are
65
+ * filters on an annotation/superclass that are annotated with {@link Indexed} are
67
66
* supported: if any other include filter is specified, the index is ignored and
68
67
* classpath scanning is used instead.
69
68
*
@@ -86,25 +85,32 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
86
85
87
86
static final String DEFAULT_RESOURCE_PATTERN = "**/*.class" ;
88
87
89
- protected final Log logger = LogFactory .getLog (getClass ());
90
-
91
- private Environment environment ;
92
-
93
- private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver ();
94
-
95
- private MetadataReaderFactory metadataReaderFactory =
96
- new CachingMetadataReaderFactory (this .resourcePatternResolver );
97
88
98
- private CandidateComponentsIndex componentsIndex ;
89
+ protected final Log logger = LogFactory . getLog ( getClass ()) ;
99
90
100
91
private String resourcePattern = DEFAULT_RESOURCE_PATTERN ;
101
92
102
93
private final List <TypeFilter > includeFilters = new LinkedList <>();
103
94
104
95
private final List <TypeFilter > excludeFilters = new LinkedList <>();
105
96
97
+ private Environment environment ;
98
+
106
99
private ConditionEvaluator conditionEvaluator ;
107
100
101
+ private ResourcePatternResolver resourcePatternResolver ;
102
+
103
+ private MetadataReaderFactory metadataReaderFactory ;
104
+
105
+ private CandidateComponentsIndex componentsIndex ;
106
+
107
+
108
+ /**
109
+ * Protected constructor for flexible subclass initialization.
110
+ * @since 4.3.6
111
+ */
112
+ protected ClassPathScanningCandidateComponentProvider () {
113
+ }
108
114
109
115
/**
110
116
* Create a ClassPathScanningCandidateComponentProvider with a {@link StandardEnvironment}.
@@ -131,74 +137,10 @@ public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, En
131
137
if (useDefaultFilters ) {
132
138
registerDefaultFilters ();
133
139
}
134
- Assert .notNull (environment , "Environment must not be null" );
135
- this .environment = environment ;
136
- }
137
-
138
-
139
- /**
140
- * Set the ResourceLoader to use for resource locations.
141
- * This will typically be a ResourcePatternResolver implementation.
142
- * <p>Default is PathMatchingResourcePatternResolver, also capable of
143
- * resource pattern resolving through the ResourcePatternResolver interface.
144
- * @see org.springframework.core.io.support.ResourcePatternResolver
145
- * @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
146
- */
147
- @ Override
148
- public void setResourceLoader (ResourceLoader resourceLoader ) {
149
- this .resourcePatternResolver = ResourcePatternUtils .getResourcePatternResolver (resourceLoader );
150
- this .metadataReaderFactory = new CachingMetadataReaderFactory (resourceLoader );
151
- this .componentsIndex = CandidateComponentsIndexLoader .loadIndex (resourceLoader .getClassLoader ());
152
- }
153
-
154
- /**
155
- * Return the ResourceLoader that this component provider uses.
156
- */
157
- public final ResourceLoader getResourceLoader () {
158
- return this .resourcePatternResolver ;
140
+ setEnvironment (environment );
141
+ setResourceLoader (null );
159
142
}
160
143
161
- /**
162
- * Set the {@link MetadataReaderFactory} to use.
163
- * <p>Default is a {@link CachingMetadataReaderFactory} for the specified
164
- * {@linkplain #setResourceLoader resource loader}.
165
- * <p>Call this setter method <i>after</i> {@link #setResourceLoader} in order
166
- * for the given MetadataReaderFactory to override the default factory.
167
- */
168
- public void setMetadataReaderFactory (MetadataReaderFactory metadataReaderFactory ) {
169
- this .metadataReaderFactory = metadataReaderFactory ;
170
- }
171
-
172
- /**
173
- * Return the MetadataReaderFactory used by this component provider.
174
- */
175
- public final MetadataReaderFactory getMetadataReaderFactory () {
176
- return this .metadataReaderFactory ;
177
- }
178
-
179
- /**
180
- * Set the Environment to use when resolving placeholders and evaluating
181
- * {@link Conditional @Conditional}-annotated component classes.
182
- * <p>The default is a {@link StandardEnvironment}.
183
- * @param environment the Environment to use
184
- */
185
- public void setEnvironment (Environment environment ) {
186
- Assert .notNull (environment , "Environment must not be null" );
187
- this .environment = environment ;
188
- this .conditionEvaluator = null ;
189
- }
190
-
191
- @ Override
192
- public final Environment getEnvironment () {
193
- return this .environment ;
194
- }
195
-
196
- /**
197
- * Returns the {@link BeanDefinitionRegistry} used by this scanner, if any.
198
- */
199
- protected BeanDefinitionRegistry getRegistry () {
200
- return null ;
201
- }
202
144
203
145
/**
204
146
* Set the resource pattern to use when scanning the classpath.
@@ -273,6 +215,70 @@ protected void registerDefaultFilters() {
273
215
}
274
216
}
275
217
218
+ /**
219
+ * Set the Environment to use when resolving placeholders and evaluating
220
+ * {@link Conditional @Conditional}-annotated component classes.
221
+ * <p>The default is a {@link StandardEnvironment}.
222
+ * @param environment the Environment to use
223
+ */
224
+ public void setEnvironment (Environment environment ) {
225
+ Assert .notNull (environment , "Environment must not be null" );
226
+ this .environment = environment ;
227
+ this .conditionEvaluator = null ;
228
+ }
229
+
230
+ @ Override
231
+ public final Environment getEnvironment () {
232
+ return this .environment ;
233
+ }
234
+
235
+ /**
236
+ * Return the {@link BeanDefinitionRegistry} used by this scanner, if any.
237
+ */
238
+ protected BeanDefinitionRegistry getRegistry () {
239
+ return null ;
240
+ }
241
+
242
+ /**
243
+ * Set the {@link ResourceLoader} to use for resource locations.
244
+ * This will typically be a {@link ResourcePatternResolver} implementation.
245
+ * <p>Default is a {@code PathMatchingResourcePatternResolver}, also capable of
246
+ * resource pattern resolving through the {@code ResourcePatternResolver} interface.
247
+ * @see org.springframework.core.io.support.ResourcePatternResolver
248
+ * @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
249
+ */
250
+ @ Override
251
+ public void setResourceLoader (ResourceLoader resourceLoader ) {
252
+ this .resourcePatternResolver = ResourcePatternUtils .getResourcePatternResolver (resourceLoader );
253
+ this .metadataReaderFactory = new CachingMetadataReaderFactory (resourceLoader );
254
+ this .componentsIndex = CandidateComponentsIndexLoader .loadIndex (this .resourcePatternResolver .getClassLoader ());
255
+ }
256
+
257
+ /**
258
+ * Return the ResourceLoader that this component provider uses.
259
+ */
260
+ public final ResourceLoader getResourceLoader () {
261
+ return this .resourcePatternResolver ;
262
+ }
263
+
264
+ /**
265
+ * Set the {@link MetadataReaderFactory} to use.
266
+ * <p>Default is a {@link CachingMetadataReaderFactory} for the specified
267
+ * {@linkplain #setResourceLoader resource loader}.
268
+ * <p>Call this setter method <i>after</i> {@link #setResourceLoader} in order
269
+ * for the given MetadataReaderFactory to override the default factory.
270
+ */
271
+ public void setMetadataReaderFactory (MetadataReaderFactory metadataReaderFactory ) {
272
+ this .metadataReaderFactory = metadataReaderFactory ;
273
+ }
274
+
275
+ /**
276
+ * Return the MetadataReaderFactory used by this component provider.
277
+ */
278
+ public final MetadataReaderFactory getMetadataReaderFactory () {
279
+ return this .metadataReaderFactory ;
280
+ }
281
+
276
282
277
283
/**
278
284
* Scan the class path for candidate components.
@@ -496,10 +502,12 @@ protected String extractStereotype(TypeFilter filter) {
496
502
}
497
503
498
504
/**
499
- * Clear the underlying metadata cache, removing all cached class metadata.
505
+ * Clear the local metadata cache, if any , removing all cached class metadata.
500
506
*/
501
507
public void clearCache () {
502
508
if (this .metadataReaderFactory instanceof CachingMetadataReaderFactory ) {
509
+ // Clear cache in externally provided MetadataReaderFactory; this is a no-op
510
+ // for a shared cache since it'll be cleared by the ApplicationContext.
503
511
((CachingMetadataReaderFactory ) this .metadataReaderFactory ).clearCache ();
504
512
}
505
513
}
0 commit comments