diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 1d20cee428..870378487c 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -233,6 +233,12 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean metadata.checkConfigMembers(beanDefinition); } + @Override + public void resetBeanDefinition(String beanName) { + this.lookupMethodsChecked.remove(beanName); + this.injectionMetadataCache.remove(beanName); + } + @Override @Nullable public Constructor[] determineCandidateConstructors(Class beanClass, final String beanName) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index a0d6fa4494..22bfb11772 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -415,8 +415,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac throws BeansException { Object result = existingBean; - for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { - Object current = beanProcessor.postProcessBeforeInitialization(result, beanName); + for (BeanPostProcessor processor : getBeanPostProcessors()) { + Object current = processor.postProcessBeforeInitialization(result, beanName); if (current == null) { return result; } @@ -430,8 +430,8 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac throws BeansException { Object result = existingBean; - for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { - Object current = beanProcessor.postProcessAfterInitialization(result, beanName); + for (BeanPostProcessor processor : getBeanPostProcessors()) { + Object current = processor.postProcessAfterInitialization(result, beanName); if (current == null) { return result; } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index ed40ab417f..5e5bf365fe 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -65,6 +65,7 @@ import org.springframework.beans.factory.SmartInitializingSingleton; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionHolder; +import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.DependencyDescriptor; @@ -969,6 +970,13 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto // (e.g. the default StaticMessageSource in a StaticApplicationContext). destroySingleton(beanName); + // Notify all post-processors that the specified bean definition has been reset. + for (BeanPostProcessor processor : getBeanPostProcessors()) { + if (processor instanceof MergedBeanDefinitionPostProcessor) { + ((MergedBeanDefinitionPostProcessor) processor).resetBeanDefinition(beanName); + } + } + // Reset all bean definitions that have the given bean as parent (recursively). for (String bdName : this.beanDefinitionNames) { if (!beanName.equals(bdName)) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/MergedBeanDefinitionPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/MergedBeanDefinitionPostProcessor.java index 02d050aead..a58b927923 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/MergedBeanDefinitionPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/MergedBeanDefinitionPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 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. @@ -45,4 +45,13 @@ public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor { */ void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class beanType, String beanName); + /** + * A notification that the bean definition for the specified name has been reset, + * and that this post-processor should clear any metadata for the affected bean. + * @param beanName the name of the bean + * @since 5.1 + */ + default void resetBeanDefinition(String beanName) { + } + } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index c650d460cb..772d5972a1 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -299,6 +299,11 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean metadata.checkConfigMembers(beanDefinition); } + @Override + public void resetBeanDefinition(String beanName) { + this.injectionMetadataCache.remove(beanName); + } + @Override public Object postProcessBeforeInstantiation(Class beanClass, String beanName) { return null; diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java index 51f8886573..060b0ed29e 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java @@ -334,6 +334,11 @@ public class PersistenceAnnotationBeanPostProcessor metadata.checkConfigMembers(beanDefinition); } + @Override + public void resetBeanDefinition(String beanName) { + this.injectionMetadataCache.remove(beanName); + } + @Override public Object postProcessBeforeInstantiation(Class beanClass, String beanName) { return null;