Cache ASM metadata at the context level (if supported)
Includes streamlined ClassPathBeanDefinitionScanner setup. Issue: SPR-14654
This commit is contained in:
@@ -38,10 +38,10 @@ import org.springframework.util.Assert;
|
||||
* deliberately override certain bean definitions via an extra {@code @Configuration}
|
||||
* class.
|
||||
*
|
||||
* <p>See @{@link Configuration} Javadoc for usage examples.
|
||||
* <p>See @{@link Configuration}'s javadoc for usage examples.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
* @since 3.0
|
||||
* @see #register
|
||||
* @see #scan
|
||||
@@ -138,12 +138,6 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
|
||||
this.scanner.setScopeMetadataResolver(scopeMetadataResolver);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void prepareRefresh() {
|
||||
this.scanner.clearCache();
|
||||
super.prepareRefresh();
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// Implementation of AnnotationConfigRegistry
|
||||
|
||||
@@ -132,16 +132,39 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
|
||||
* @since 3.1
|
||||
* @see #setResourceLoader
|
||||
*/
|
||||
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment) {
|
||||
super(useDefaultFilters, environment);
|
||||
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
|
||||
Environment environment) {
|
||||
|
||||
this(registry, useDefaultFilters, environment,
|
||||
(registry instanceof ResourceLoader ? (ResourceLoader) registry : null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code ClassPathBeanDefinitionScanner} for the given bean factory and
|
||||
* using the given {@link Environment} when evaluating bean definition profile metadata.
|
||||
* @param registry the {@code BeanFactory} to load bean definitions into, in the form
|
||||
* of a {@code BeanDefinitionRegistry}
|
||||
* @param useDefaultFilters whether to include the default filters for the
|
||||
* {@link org.springframework.stereotype.Component @Component},
|
||||
* {@link org.springframework.stereotype.Repository @Repository},
|
||||
* {@link org.springframework.stereotype.Service @Service}, and
|
||||
* {@link org.springframework.stereotype.Controller @Controller} stereotype annotations
|
||||
* @param environment the Spring {@link Environment} to use when evaluating bean
|
||||
* definition profile metadata
|
||||
* @param resourceLoader the {@link ResourceLoader} to use
|
||||
* @since 4.3.6
|
||||
*/
|
||||
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
|
||||
Environment environment, ResourceLoader resourceLoader) {
|
||||
|
||||
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
|
||||
this.registry = registry;
|
||||
|
||||
// Determine ResourceLoader to use.
|
||||
if (this.registry instanceof ResourceLoader) {
|
||||
setResourceLoader((ResourceLoader) this.registry);
|
||||
if (useDefaultFilters) {
|
||||
registerDefaultFilters();
|
||||
}
|
||||
setEnvironment(environment);
|
||||
setResourceLoader(resourceLoader);
|
||||
}
|
||||
|
||||
|
||||
@@ -192,7 +215,8 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
|
||||
* @see #setScopedProxyMode
|
||||
*/
|
||||
public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver) {
|
||||
this.scopeMetadataResolver = (scopeMetadataResolver != null ? scopeMetadataResolver : new AnnotationScopeMetadataResolver());
|
||||
this.scopeMetadataResolver =
|
||||
(scopeMetadataResolver != null ? scopeMetadataResolver : new AnnotationScopeMetadataResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -258,7 +282,8 @@ public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateCo
|
||||
}
|
||||
if (checkCandidate(beanName, candidate)) {
|
||||
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
|
||||
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
|
||||
definitionHolder =
|
||||
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
|
||||
beanDefinitions.add(definitionHolder);
|
||||
registerBeanDefinition(definitionHolder, this.registry);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ import org.springframework.core.env.EnvironmentCapable;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternUtils;
|
||||
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
|
||||
@@ -63,7 +62,7 @@ import org.springframework.util.ClassUtils;
|
||||
* use {@link CandidateComponentsIndex the index} if it is available of scans the
|
||||
* classpath otherwise. Candidate components are identified by applying exclude and
|
||||
* include filters. {@link AnnotationTypeFilter}, {@link AssignableTypeFilter} include
|
||||
* filters on an annotation/super-class that are annotated with {@link Indexed} are
|
||||
* filters on an annotation/superclass that are annotated with {@link Indexed} are
|
||||
* supported: if any other include filter is specified, the index is ignored and
|
||||
* classpath scanning is used instead.
|
||||
*
|
||||
@@ -86,25 +85,32 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
|
||||
static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";
|
||||
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
private Environment environment;
|
||||
|
||||
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
|
||||
|
||||
private MetadataReaderFactory metadataReaderFactory =
|
||||
new CachingMetadataReaderFactory(this.resourcePatternResolver);
|
||||
|
||||
private CandidateComponentsIndex componentsIndex;
|
||||
|
||||
private String resourcePattern = DEFAULT_RESOURCE_PATTERN;
|
||||
|
||||
private final List<TypeFilter> includeFilters = new LinkedList<>();
|
||||
|
||||
private final List<TypeFilter> excludeFilters = new LinkedList<>();
|
||||
|
||||
private Environment environment;
|
||||
|
||||
private ConditionEvaluator conditionEvaluator;
|
||||
|
||||
private ResourcePatternResolver resourcePatternResolver;
|
||||
|
||||
private MetadataReaderFactory metadataReaderFactory;
|
||||
|
||||
private CandidateComponentsIndex componentsIndex;
|
||||
|
||||
|
||||
/**
|
||||
* Protected constructor for flexible subclass initialization.
|
||||
* @since 4.3.6
|
||||
*/
|
||||
protected ClassPathScanningCandidateComponentProvider() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a ClassPathScanningCandidateComponentProvider with a {@link StandardEnvironment}.
|
||||
@@ -131,75 +137,11 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
if (useDefaultFilters) {
|
||||
registerDefaultFilters();
|
||||
}
|
||||
Assert.notNull(environment, "Environment must not be null");
|
||||
this.environment = environment;
|
||||
setEnvironment(environment);
|
||||
setResourceLoader(null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the ResourceLoader to use for resource locations.
|
||||
* This will typically be a ResourcePatternResolver implementation.
|
||||
* <p>Default is PathMatchingResourcePatternResolver, also capable of
|
||||
* resource pattern resolving through the ResourcePatternResolver interface.
|
||||
* @see org.springframework.core.io.support.ResourcePatternResolver
|
||||
* @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
|
||||
*/
|
||||
@Override
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
|
||||
this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);
|
||||
this.componentsIndex = CandidateComponentsIndexLoader.loadIndex(resourceLoader.getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 Conditional @Conditional}-annotated component classes.
|
||||
* <p>The default is a {@link StandardEnvironment}.
|
||||
* @param environment the Environment to use
|
||||
*/
|
||||
public void setEnvironment(Environment environment) {
|
||||
Assert.notNull(environment, "Environment must not be null");
|
||||
this.environment = environment;
|
||||
this.conditionEvaluator = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Environment getEnvironment() {
|
||||
return this.environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link BeanDefinitionRegistry} used by this scanner, if any.
|
||||
*/
|
||||
protected BeanDefinitionRegistry getRegistry() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the resource pattern to use when scanning the classpath.
|
||||
* This value will be appended to each base package name.
|
||||
@@ -273,6 +215,70 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Environment to use when resolving placeholders and evaluating
|
||||
* {@link Conditional @Conditional}-annotated component classes.
|
||||
* <p>The default is a {@link StandardEnvironment}.
|
||||
* @param environment the Environment to use
|
||||
*/
|
||||
public void setEnvironment(Environment environment) {
|
||||
Assert.notNull(environment, "Environment must not be null");
|
||||
this.environment = environment;
|
||||
this.conditionEvaluator = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final Environment getEnvironment() {
|
||||
return this.environment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link BeanDefinitionRegistry} used by this scanner, if any.
|
||||
*/
|
||||
protected BeanDefinitionRegistry getRegistry() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link ResourceLoader} to use for resource locations.
|
||||
* This will typically be a {@link ResourcePatternResolver} implementation.
|
||||
* <p>Default is a {@code PathMatchingResourcePatternResolver}, also capable of
|
||||
* resource pattern resolving through the {@code ResourcePatternResolver} interface.
|
||||
* @see org.springframework.core.io.support.ResourcePatternResolver
|
||||
* @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
|
||||
*/
|
||||
@Override
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
|
||||
this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);
|
||||
this.componentsIndex = CandidateComponentsIndexLoader.loadIndex(this.resourcePatternResolver.getClassLoader());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Scan the class path for candidate components.
|
||||
@@ -496,10 +502,12 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the underlying metadata cache, removing all cached class metadata.
|
||||
* Clear the local metadata cache, if any, removing all cached class metadata.
|
||||
*/
|
||||
public void clearCache() {
|
||||
if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) {
|
||||
// Clear cache in externally provided MetadataReaderFactory; this is a no-op
|
||||
// for a shared cache since it'll be cleared by the ApplicationContext.
|
||||
((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,10 +77,8 @@ class ComponentScanAnnotationParser {
|
||||
Assert.state(this.environment != null, "Environment must not be null");
|
||||
Assert.state(this.resourceLoader != null, "ResourceLoader must not be null");
|
||||
|
||||
ClassPathBeanDefinitionScanner scanner =
|
||||
new ClassPathBeanDefinitionScanner(this.registry, componentScan.getBoolean("useDefaultFilters"));
|
||||
scanner.setEnvironment(this.environment);
|
||||
scanner.setResourceLoader(this.resourceLoader);
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
|
||||
componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);
|
||||
|
||||
Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
|
||||
boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
|
||||
|
||||
@@ -99,8 +99,6 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
// Delegate bean definition registration to scanner class.
|
||||
ClassPathBeanDefinitionScanner scanner = createScanner(parserContext.getReaderContext(), useDefaultFilters);
|
||||
scanner.setResourceLoader(parserContext.getReaderContext().getResourceLoader());
|
||||
scanner.setEnvironment(parserContext.getReaderContext().getEnvironment());
|
||||
scanner.setBeanDefinitionDefaults(parserContext.getDelegate().getBeanDefinitionDefaults());
|
||||
scanner.setAutowireCandidatePatterns(parserContext.getDelegate().getAutowireCandidatePatterns());
|
||||
|
||||
@@ -128,7 +126,8 @@ public class ComponentScanBeanDefinitionParser implements BeanDefinitionParser {
|
||||
}
|
||||
|
||||
protected ClassPathBeanDefinitionScanner createScanner(XmlReaderContext readerContext, boolean useDefaultFilters) {
|
||||
return new ClassPathBeanDefinitionScanner(readerContext.getRegistry(), useDefaultFilters);
|
||||
return new ClassPathBeanDefinitionScanner(readerContext.getRegistry(), useDefaultFilters,
|
||||
readerContext.getEnvironment(), readerContext.getResourceLoader());
|
||||
}
|
||||
|
||||
protected void registerComponents(
|
||||
|
||||
@@ -350,6 +350,8 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
}
|
||||
|
||||
if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) {
|
||||
// Clear cache in externally provided MetadataReaderFactory; this is a no-op
|
||||
// for a shared cache since it'll be cleared by the ApplicationContext.
|
||||
((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,11 @@ import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
*/
|
||||
public class CandidateComponentsIndexLoader {
|
||||
|
||||
|
||||
private static final Log logger = LogFactory.getLog(CandidateComponentsIndexLoader.class);
|
||||
/**
|
||||
* The location to look for components.
|
||||
* <p>Can be present in multiple JAR files.
|
||||
*/
|
||||
public static final String COMPONENTS_RESOURCE_LOCATION = "META-INF/spring.components";
|
||||
|
||||
/**
|
||||
* System property that instructs Spring to ignore the index, i.e.
|
||||
@@ -54,18 +57,13 @@ public class CandidateComponentsIndexLoader {
|
||||
*/
|
||||
public static final String IGNORE_INDEX = "spring.index.ignore";
|
||||
|
||||
private static final boolean shouldIgnoreIndex =
|
||||
SpringProperties.getFlag(IGNORE_INDEX);
|
||||
|
||||
private static final boolean shouldIgnoreIndex = SpringProperties.getFlag(IGNORE_INDEX);
|
||||
|
||||
/**
|
||||
* The location to look for components.
|
||||
* <p>Can be present in multiple JAR files.
|
||||
*/
|
||||
public static final String COMPONENTS_RESOURCE_LOCATION = "META-INF/spring.components";
|
||||
private static final Log logger = LogFactory.getLog(CandidateComponentsIndexLoader.class);
|
||||
|
||||
private static final ConcurrentMap<ClassLoader, CandidateComponentsIndex> cache
|
||||
= new ConcurrentReferenceHashMap<>();
|
||||
private static final ConcurrentMap<ClassLoader, CandidateComponentsIndex> cache =
|
||||
new ConcurrentReferenceHashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
@@ -107,8 +105,8 @@ public class CandidateComponentsIndexLoader {
|
||||
return (totalCount > 0 ? new CandidateComponentsIndex(result) : null);
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new IllegalArgumentException("Unable to load indexes from location ["
|
||||
+ COMPONENTS_RESOURCE_LOCATION + "]", ex);
|
||||
throw new IllegalArgumentException("Unable to load indexes from location [" +
|
||||
COMPONENTS_RESOURCE_LOCATION + "]", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -872,6 +872,9 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
||||
* {@link org.springframework.context.event.ContextRefreshedEvent}.
|
||||
*/
|
||||
protected void finishRefresh() {
|
||||
// Clear context-level resource caches (such as ASM metadata).
|
||||
clearResourceCaches();
|
||||
|
||||
// Initialize lifecycle processor for this context.
|
||||
initLifecycleProcessor();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user