159
159
public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationAwareBeanPostProcessor ,
160
160
MergedBeanDefinitionPostProcessor , BeanRegistrationAotProcessor , PriorityOrdered , BeanFactoryAware {
161
161
162
+ private static final Constructor <?>[] EMPTY_CONSTRUCTOR_ARRAY = new Constructor <?>[0 ];
163
+
164
+
162
165
protected final Log logger = LogFactory .getLog (getClass ());
163
166
164
167
private final Set <Class <? extends Annotation >> autowiredAnnotationTypes = new LinkedHashSet <>(4 );
@@ -286,7 +289,25 @@ public void setBeanFactory(BeanFactory beanFactory) {
286
289
287
290
@ Override
288
291
public void postProcessMergedBeanDefinition (RootBeanDefinition beanDefinition , Class <?> beanType , String beanName ) {
292
+ // Register externally managed config members on bean definition.
289
293
findInjectionMetadata (beanName , beanType , beanDefinition );
294
+
295
+ // Use opportunity to clear caches which are not needed after singleton instantiation.
296
+ // The injectionMetadataCache itself is left intact since it cannot be reliably
297
+ // reconstructed in terms of externally managed config members otherwise.
298
+ if (beanDefinition .isSingleton ()) {
299
+ this .candidateConstructorsCache .remove (beanType );
300
+ // With actual lookup overrides, keep it intact along with bean definition.
301
+ if (!beanDefinition .hasMethodOverrides ()) {
302
+ this .lookupMethodsChecked .remove (beanName );
303
+ }
304
+ }
305
+ }
306
+
307
+ @ Override
308
+ public void resetBeanDefinition (String beanName ) {
309
+ this .lookupMethodsChecked .remove (beanName );
310
+ this .injectionMetadataCache .remove (beanName );
290
311
}
291
312
292
313
@ Override
@@ -324,12 +345,6 @@ private InjectionMetadata findInjectionMetadata(String beanName, Class<?> beanTy
324
345
return metadata ;
325
346
}
326
347
327
- @ Override
328
- public void resetBeanDefinition (String beanName ) {
329
- this .lookupMethodsChecked .remove (beanName );
330
- this .injectionMetadataCache .remove (beanName );
331
- }
332
-
333
348
@ Override
334
349
public Class <?> determineBeanType (Class <?> beanClass , String beanName ) throws BeanCreationException {
335
350
checkLookupMethods (beanClass , beanName );
@@ -429,7 +444,7 @@ else if (candidates.size() == 1 && logger.isInfoEnabled()) {
429
444
"default constructor to fall back to: " + candidates .get (0 ));
430
445
}
431
446
}
432
- candidateConstructors = candidates .toArray (new Constructor <?>[ 0 ] );
447
+ candidateConstructors = candidates .toArray (EMPTY_CONSTRUCTOR_ARRAY );
433
448
}
434
449
else if (rawCandidates .length == 1 && rawCandidates [0 ].getParameterCount () > 0 ) {
435
450
candidateConstructors = new Constructor <?>[] {rawCandidates [0 ]};
@@ -442,7 +457,7 @@ else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {
442
457
candidateConstructors = new Constructor <?>[] {primaryConstructor };
443
458
}
444
459
else {
445
- candidateConstructors = new Constructor <?>[ 0 ] ;
460
+ candidateConstructors = EMPTY_CONSTRUCTOR_ARRAY ;
446
461
}
447
462
this .candidateConstructorsCache .put (beanClass , candidateConstructors );
448
463
}
0 commit comments