上一篇文章介紹了spring的自動配置,那麼spring是如何掃描包下所有類資訊?本篇學習一下關於@ComponentScan、@ComponentScans註解解析和ClassPathBeanDefinitionScanner、PathMatchingResourcePatternResolver類的原始碼分析。
ClassPathBeanDefinitionScanner
A bean definition scanner that detects bean candidates on the classpath,
registering corresponding bean definitions with a given registry ({@code BeanFactory}
or {@code ApplicationContext}).
複製程式碼
一個bean定義掃描器用來檢測類路徑下候選bean,提供一個註冊器(registry)註冊相應的bean定義
PathMatchingResourcePatternResolver
A {@link ResourcePatternResolver} implementation that is able to resolve a specified resource location path into one or more matching Resources.
The source path may be a simple path which has a one-to-one mapping to a target {@link org.springframework.core.io.Resource}, or alternatively
may contain the special "{@code classpath*:}" prefix and/orinternal Ant-style regular expressions (matched using Spring's{@link org.springframework.util.AntPathMatcher} utility).Both of the latter are effectively wildcards.
複製程式碼
一個ResourcePatternResolver的實現,可以為一個或者更多的資源解析指定的資源匹配位置路徑;源路徑可能是一個簡單的路徑一對一的對映目標或者另外可能含有特殊的字首和基於ant正規表示式
大致瞭解兩個關鍵類後,從註解解析入口開始進行原始碼分析
@ComponentScan 註解解析
在上一篇文章中瞭解到註解解析是在ConfigurationClassParser的doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)方法中進行
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
throws IOException {
......
// 處理 @ComponentScan 註解
//獲取資源類的ComponentScan、ComponentScans註解資訊
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
if (!componentScans.isEmpty() &&
!this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
for (AnnotationAttributes componentScan : componentScans) {
// 配置類上有@ComponentScan註解,立即執行掃描
Set<BeanDefinitionHolder> scannedBeanDefinitions =
this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
// Check the set of scanned definitions for any further config classes and parse recursively if needed
for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
if (ConfigurationClassUtils.checkConfigurationClassCandidate(
holder.getBeanDefinition(), this.metadataReaderFactory)) {
parse(holder.getBeanDefinition().getBeanClassName(), holder.getBeanName());
}
}
}
}
......
return null;
}
複製程式碼
這裡的this.componentScanParser 是 ComponentScanAnnotationParser
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
Assert.state(this.environment != null, "Environment must not be null");
Assert.state(this.resourceLoader != null, "ResourceLoader must not be null");
//類路徑Bean定義掃描器
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);
...... 省略讀取註解資訊,為ClassPathBeanDefinitionScanner初始化賦值
//掃描器掃描
return scanner.doScan(StringUtils.toStringArray(basePackages));
複製程式碼
ClassPathBeanDefinitionScanner掃描
指定包進行掃描
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<BeanDefinitionHolder>();
for (String basePackage : basePackages) {
//通過包路徑尋找候選元件
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
for (BeanDefinition candidate : candidates) {
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
if (candidate instanceof AbstractBeanDefinition) {
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
}
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
}
if (checkCandidate(beanName, candidate)) {
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
definitionHolder =
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
beanDefinitions.add(definitionHolder);
registerBeanDefinition(definitionHolder, this.registry);
}
}
}
return beanDefinitions;
}
複製程式碼
通過包路徑尋找候選元件
掃描classpath下的候選元件
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
Set<BeanDefinition> candidates = new LinkedHashSet<BeanDefinition>();
try {
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolveBasePackage(basePackage) + '/' + this.resourcePattern;
//通過資源解析器獲取resources
Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath);
boolean traceEnabled = logger.isTraceEnabled();
boolean debugEnabled = logger.isDebugEnabled();
for (Resource resource : resources) {
if (traceEnabled) {
logger.trace("Scanning " + resource);
}
if (resource.isReadable()) {
try {
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);
if (isCandidateComponent(metadataReader)) {
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
sbd.setResource(resource);
sbd.setSource(resource);
if (isCandidateComponent(sbd)) {
if (debugEnabled) {
logger.debug("Identified candidate component class: " + resource);
}
candidates.add(sbd);
}
else {
if (debugEnabled) {
logger.debug("Ignored because not a concrete top-level class: " + resource);
}
}
}
else {
if (traceEnabled) {
logger.trace("Ignored because not matching any filter: " + resource);
}
}
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(
"Failed to read candidate component class: " + resource, ex);
}
}
else {
if (traceEnabled) {
logger.trace("Ignored because not readable: " + resource);
}
}
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
}
return candidates;
複製程式碼
這裡的this.resourcePatternResolver是通過PathMatchingResourcePatternResolver獲取resources;通過MetaDataReader例項化ScannedGenericBeanDefinition。
以上屬於原創文章,轉載請註明作者@怪咖
QQ:208275451