1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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.
@@ -344,24 +344,31 @@ private boolean isInfrastructureBean(String beanName) {
344
344
345
345
346
346
/**
347
- * BeanPostProcessor that detects beans which implement the ApplicationListener interface.
348
- * This catches beans that can't reliably be detected by getBeanNamesForType.
347
+ * {@code BeanPostProcessor} that detects beans which implement the {@code ApplicationListener}
348
+ * interface. This catches beans that can't reliably be detected by {@code getBeanNamesForType}
349
+ * and related operations which only work against top-level beans.
350
+ *
351
+ * <p>With standard Java serialization, this post-processor won't get serialized as part of
352
+ * {@code DisposableBeanAdapter} to begin with. However, with alternative serialization
353
+ * mechanisms, {@code DisposableBeanAdapter.writeReplace} might not get used at all, so we
354
+ * defensively mark this post-processor's field state as {@code transient}.
349
355
*/
350
- private static class ApplicationListenerDetector implements MergedBeanDefinitionPostProcessor , DestructionAwareBeanPostProcessor {
356
+ private static class ApplicationListenerDetector
357
+ implements DestructionAwareBeanPostProcessor , MergedBeanDefinitionPostProcessor {
351
358
352
359
private static final Log logger = LogFactory .getLog (ApplicationListenerDetector .class );
353
360
354
- private final AbstractApplicationContext applicationContext ;
361
+ private transient final AbstractApplicationContext applicationContext ;
355
362
356
- private final Map <String , Boolean > singletonNames = new ConcurrentHashMap <String , Boolean >(256 );
363
+ private transient final Map <String , Boolean > singletonNames = new ConcurrentHashMap <String , Boolean >(256 );
357
364
358
365
public ApplicationListenerDetector (AbstractApplicationContext applicationContext ) {
359
366
this .applicationContext = applicationContext ;
360
367
}
361
368
362
369
@ Override
363
370
public void postProcessMergedBeanDefinition (RootBeanDefinition beanDefinition , Class <?> beanType , String beanName ) {
364
- if (beanDefinition .isSingleton ()) {
371
+ if (this . applicationContext != null && beanDefinition .isSingleton ()) {
365
372
this .singletonNames .put (beanName , Boolean .TRUE );
366
373
}
367
374
}
@@ -373,7 +380,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
373
380
374
381
@ Override
375
382
public Object postProcessAfterInitialization (Object bean , String beanName ) {
376
- if (bean instanceof ApplicationListener ) {
383
+ if (this . applicationContext != null && bean instanceof ApplicationListener ) {
377
384
// potentially not detected as a listener by getBeanNamesForType retrieval
378
385
Boolean flag = this .singletonNames .get (beanName );
379
386
if (Boolean .TRUE .equals (flag )) {
0 commit comments