Skip to content

Commit e0734ae

Browse files
committed
ApplicationListenerDetector explicitly prevents serialization of its ApplicationContext reference
Issue: SPR-14214
1 parent 6ab8d36 commit e0734ae

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -344,25 +344,31 @@ private boolean isInfrastructureBean(String beanName) {
344344

345345

346346
/**
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}.
349355
*/
350356
private static class ApplicationListenerDetector
351357
implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor {
352358

353359
private static final Log logger = LogFactory.getLog(ApplicationListenerDetector.class);
354360

355-
private final AbstractApplicationContext applicationContext;
361+
private transient final AbstractApplicationContext applicationContext;
356362

357-
private final Map<String, Boolean> singletonNames = new ConcurrentHashMap<String, Boolean>(256);
363+
private transient final Map<String, Boolean> singletonNames = new ConcurrentHashMap<String, Boolean>(256);
358364

359365
public ApplicationListenerDetector(AbstractApplicationContext applicationContext) {
360366
this.applicationContext = applicationContext;
361367
}
362368

363369
@Override
364370
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
365-
if (beanDefinition.isSingleton()) {
371+
if (this.applicationContext != null && beanDefinition.isSingleton()) {
366372
this.singletonNames.put(beanName, Boolean.TRUE);
367373
}
368374
}
@@ -374,7 +380,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
374380

375381
@Override
376382
public Object postProcessAfterInitialization(Object bean, String beanName) {
377-
if (bean instanceof ApplicationListener) {
383+
if (this.applicationContext != null && bean instanceof ApplicationListener) {
378384
// potentially not detected as a listener by getBeanNamesForType retrieval
379385
Boolean flag = this.singletonNames.get(beanName);
380386
if (Boolean.TRUE.equals(flag)) {

0 commit comments

Comments
 (0)