Several enhancements with respect to CachingMetadataReaderFactory handling.
Added "clearCache()" method to CachingMetadataReaderFactory, for clearing the metadata cache once not needed anymore - in particular when the MetadataReaderFactory instance is long-lived. Also added "setMetadataReaderFactory" method to ClassPathScanningCandidateComponentProvider, analogous to ConfigurationClassPostProcessor.
This commit is contained in:
@@ -153,4 +153,10 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
|
||||
this.scanner.scan(basePackages);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void prepareRefresh() {
|
||||
this.scanner.clearCache();
|
||||
super.prepareRefresh();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
@@ -78,7 +79,8 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
|
||||
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
|
||||
|
||||
private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
|
||||
private MetadataReaderFactory metadataReaderFactory =
|
||||
new CachingMetadataReaderFactory(this.resourcePatternResolver);
|
||||
|
||||
private String resourcePattern = DEFAULT_RESOURCE_PATTERN;
|
||||
|
||||
@@ -120,6 +122,31 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the ResourceLoader that this component provider uses.
|
||||
*/
|
||||
public final ResourceLoader getResourceLoader() {
|
||||
return this.resourcePatternResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link MetadataReaderFactory} to use.
|
||||
* <p>Default is a {@link CachingMetadataReaderFactory} for the specified
|
||||
* {@linkplain #setResourceLoader resource loader}.
|
||||
* <p>Call this setter method <i>after</i> {@link #setResourceLoader} in order
|
||||
* for the given MetadataReaderFactory to override the default factory.
|
||||
*/
|
||||
public void setMetadataReaderFactory(MetadataReaderFactory metadataReaderFactory) {
|
||||
this.metadataReaderFactory = metadataReaderFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the MetadataReaderFactory used by this component provider.
|
||||
*/
|
||||
public final MetadataReaderFactory getMetadataReaderFactory() {
|
||||
return this.metadataReaderFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Environment to use when resolving placeholders and evaluating
|
||||
* {@link Profile @Profile}-annotated component classes.
|
||||
@@ -130,17 +157,10 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
public Environment getEnvironment() {
|
||||
public final Environment getEnvironment() {
|
||||
return this.environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the ResourceLoader that this component provider uses.
|
||||
*/
|
||||
public final ResourceLoader getResourceLoader() {
|
||||
return this.resourcePatternResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the resource pattern to use when scanning the classpath.
|
||||
* This value will be appended to each base package name.
|
||||
@@ -324,4 +344,14 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
return (beanDefinition.getMetadata().isConcrete() && beanDefinition.getMetadata().isIndependent());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clear the underlying metadata cache, removing all cached class metadata.
|
||||
*/
|
||||
public void clearCache() {
|
||||
if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) {
|
||||
((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -320,6 +320,10 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
singletonRegistry.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
|
||||
}
|
||||
}
|
||||
|
||||
if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) {
|
||||
((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user