Skip to content

Commit 75a8f5b

Browse files
committed
ApplicationListenerDetector explicitly prevents serialization of its ApplicationContext reference
Issue: SPR-14214 (cherry picked from commit e0734ae)
1 parent 6a3d494 commit 75a8f5b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

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

Lines changed: 15 additions & 8 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,24 +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
*/
350-
private static class ApplicationListenerDetector implements MergedBeanDefinitionPostProcessor, DestructionAwareBeanPostProcessor {
356+
private static class ApplicationListenerDetector
357+
implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor {
351358

352359
private static final Log logger = LogFactory.getLog(ApplicationListenerDetector.class);
353360

354-
private final AbstractApplicationContext applicationContext;
361+
private transient final AbstractApplicationContext applicationContext;
355362

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);
357364

358365
public ApplicationListenerDetector(AbstractApplicationContext applicationContext) {
359366
this.applicationContext = applicationContext;
360367
}
361368

362369
@Override
363370
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
364-
if (beanDefinition.isSingleton()) {
371+
if (this.applicationContext != null && beanDefinition.isSingleton()) {
365372
this.singletonNames.put(beanName, Boolean.TRUE);
366373
}
367374
}
@@ -373,7 +380,7 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
373380

374381
@Override
375382
public Object postProcessAfterInitialization(Object bean, String beanName) {
376-
if (bean instanceof ApplicationListener) {
383+
if (this.applicationContext != null && bean instanceof ApplicationListener) {
377384
// potentially not detected as a listener by getBeanNamesForType retrieval
378385
Boolean flag = this.singletonNames.get(beanName);
379386
if (Boolean.TRUE.equals(flag)) {

0 commit comments

Comments
 (0)