Merged bean definitions are now cached early and selectively evicted after post-processing and before actual bean creation
Issue: SPR-12236
This commit is contained in:
parent
37f74e76f6
commit
09eb492079
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -121,6 +121,18 @@ public interface ConfigurableListableBeanFactory
|
||||||
*/
|
*/
|
||||||
Iterator<String> getBeanNamesIterator();
|
Iterator<String> getBeanNamesIterator();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the merged bean definition cache, removing entries for beans
|
||||||
|
* which are not considered eligible for full metadata caching yet.
|
||||||
|
* <p>Typically triggered after changes to the original bean definitions,
|
||||||
|
* e.g. after applying a {@link BeanFactoryPostProcessor}. Note that metadata
|
||||||
|
* for beans which have already been created at this point will be kept around.
|
||||||
|
* @since 4.2
|
||||||
|
* @see #getBeanDefinition
|
||||||
|
* @see #getMergedBeanDefinition
|
||||||
|
*/
|
||||||
|
void clearMetadataCache();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Freeze all bean definitions, signalling that the registered bean definitions
|
* Freeze all bean definitions, signalling that the registered bean definitions
|
||||||
* will not be modified or post-processed any further.
|
* will not be modified or post-processed any further.
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
@ -1262,7 +1263,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||||
|
|
||||||
// Only cache the merged bean definition if we're already about to create an
|
// Only cache the merged bean definition if we're already about to create an
|
||||||
// instance of the bean, or at least have already created an instance before.
|
// instance of the bean, or at least have already created an instance before.
|
||||||
if (containingBd == null && isCacheBeanMetadata() && isBeanEligibleForMetadataCaching(beanName)) {
|
if (containingBd == null && isCacheBeanMetadata()) {
|
||||||
this.mergedBeanDefinitions.put(beanName, mbd);
|
this.mergedBeanDefinitions.put(beanName, mbd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1296,6 +1297,23 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||||
this.mergedBeanDefinitions.remove(beanName);
|
this.mergedBeanDefinitions.remove(beanName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the merged bean definition cache, removing entries for beans
|
||||||
|
* which are not considered eligible for full metadata caching yet.
|
||||||
|
* <p>Typically triggered after changes to the original bean definitions,
|
||||||
|
* e.g. after applying a {@code BeanFactoryPostProcessor}. Note that metadata
|
||||||
|
* for beans which have already been created at this point will be kept around.
|
||||||
|
* @since 4.2
|
||||||
|
*/
|
||||||
|
public void clearMetadataCache() {
|
||||||
|
Iterator<String> mergedBeans = this.mergedBeanDefinitions.keySet().iterator();
|
||||||
|
while (mergedBeans.hasNext()) {
|
||||||
|
if (!isBeanEligibleForMetadataCaching(mergedBeans.next())) {
|
||||||
|
mergedBeans.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve the bean class for the specified bean definition,
|
* Resolve the bean class for the specified bean definition,
|
||||||
* resolving a bean class name into a Class reference (if necessary)
|
* resolving a bean class name into a Class reference (if necessary)
|
||||||
|
|
@ -1475,6 +1493,10 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
|
||||||
protected void markBeanAsCreated(String beanName) {
|
protected void markBeanAsCreated(String beanName) {
|
||||||
if (!this.alreadyCreated.contains(beanName)) {
|
if (!this.alreadyCreated.contains(beanName)) {
|
||||||
this.alreadyCreated.add(beanName);
|
this.alreadyCreated.add(beanName);
|
||||||
|
|
||||||
|
// Let the bean definition get re-merged now that we're actually creating
|
||||||
|
// the bean... just in case some of its metadata changed in the meantime.
|
||||||
|
clearMergedBeanDefinition(beanName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -708,6 +708,12 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||||
return iterator;
|
return iterator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearMetadataCache() {
|
||||||
|
super.clearMetadataCache();
|
||||||
|
clearByTypeCache();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void freezeConfiguration() {
|
public void freezeConfiguration() {
|
||||||
this.configurationFrozen = true;
|
this.configurationFrozen = true;
|
||||||
|
|
|
||||||
|
|
@ -179,6 +179,10 @@ class PostProcessorRegistrationDelegate {
|
||||||
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
|
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
|
||||||
}
|
}
|
||||||
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
|
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
|
||||||
|
|
||||||
|
// Clear cached merged bean definitions since the post-processors might have
|
||||||
|
// modified the original metadata, e.g. replacing placeholders in values...
|
||||||
|
beanFactory.clearMetadataCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void registerBeanPostProcessors(
|
public static void registerBeanPostProcessors(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue