From 75a8f5b3cacdc89d0ac0e60335274988dd36ea0d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 27 Apr 2016 21:30:46 +0200 Subject: [PATCH] ApplicationListenerDetector explicitly prevents serialization of its ApplicationContext reference Issue: SPR-14214 (cherry picked from commit e0734ae) --- .../PostProcessorRegistrationDelegate.java | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java b/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java index aac3383938d..70fd2e833b6 100644 --- a/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java +++ b/spring-context/src/main/java/org/springframework/context/support/PostProcessorRegistrationDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -344,16 +344,23 @@ class PostProcessorRegistrationDelegate { /** - * BeanPostProcessor that detects beans which implement the ApplicationListener interface. - * This catches beans that can't reliably be detected by getBeanNamesForType. + * {@code BeanPostProcessor} that detects beans which implement the {@code ApplicationListener} + * interface. This catches beans that can't reliably be detected by {@code getBeanNamesForType} + * and related operations which only work against top-level beans. + * + *

With standard Java serialization, this post-processor won't get serialized as part of + * {@code DisposableBeanAdapter} to begin with. However, with alternative serialization + * mechanisms, {@code DisposableBeanAdapter.writeReplace} might not get used at all, so we + * defensively mark this post-processor's field state as {@code transient}. */ - private static class ApplicationListenerDetector implements MergedBeanDefinitionPostProcessor, DestructionAwareBeanPostProcessor { + private static class ApplicationListenerDetector + implements DestructionAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor { private static final Log logger = LogFactory.getLog(ApplicationListenerDetector.class); - private final AbstractApplicationContext applicationContext; + private transient final AbstractApplicationContext applicationContext; - private final Map singletonNames = new ConcurrentHashMap(256); + private transient final Map singletonNames = new ConcurrentHashMap(256); public ApplicationListenerDetector(AbstractApplicationContext applicationContext) { this.applicationContext = applicationContext; @@ -361,7 +368,7 @@ class PostProcessorRegistrationDelegate { @Override public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName) { - if (beanDefinition.isSingleton()) { + if (this.applicationContext != null && beanDefinition.isSingleton()) { this.singletonNames.put(beanName, Boolean.TRUE); } } @@ -373,7 +380,7 @@ class PostProcessorRegistrationDelegate { @Override public Object postProcessAfterInitialization(Object bean, String beanName) { - if (bean instanceof ApplicationListener) { + if (this.applicationContext != null && bean instanceof ApplicationListener) { // potentially not detected as a listener by getBeanNamesForType retrieval Boolean flag = this.singletonNames.get(beanName); if (Boolean.TRUE.equals(flag)) {