ConfigurationClassPostProcessor
0.预备
1.在spring源码中讲到, 在refresh()方法中的第5个小方法invokeBeanFactoryPostProcessors(beanFactory)中会先后调用BeanFactoryPostProcessor接口的子接口BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry(registry)方法以及BeanFactoryPostProcessor接口的postProcessBeanFactory(beanFactory)方法;
2.spring内置的BeanDefinitionRegistryPostProcessor中有一个重要的实现类 --> ConfigurationClassPostProcessor, 本文详细讲解ConfigurationClassPostProcessor的postProcessBeanDefinitionRegistry(registry)方法和postProcessBeanFactory(beanFactory)方法
启动类
public class ConfigStart {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Student student = applicationContext.getBean(Student.class);
}
}
主配置类
@Configuration
public class MainConfig {
@Bean
public Student student() {
return new Student();
}
}
bean
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
/**
* id
*/
private Long id;
/**
* name
*/
private String name;
}
1.postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)方法解析
/**
* Derive further bean definitions from the configuration classes in the registry.
* registry 这里的registery是DefaultListableBeanFactory
*/
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
// 1.基本校验, 略过
int registryId = System.identityHashCode(registry);
if (this.registriesPostProcessed.contains(registryId)) {
throw new IllegalStateException(
"postProcessBeanDefinitionRegistry already called on this post-processor against " + registry);
}
if (this.factoriesPostProcessed.contains(registryId)) {
throw new IllegalStateException(
"postProcessBeanFactory already called on this post-processor against " + registry);
}
this.registriesPostProcessed.add(registryId);
// 2.核心方法 见@1.1
processConfigBeanDefinitions(registry);
}
1.1 processConfigBeanDefinitions(BeanDefinitionRegistry registry)方法解析
/**
* Build and validate a configuration model based on the registry of
* {@link Configuration} classes.
* 翻译: 构建和验证基于registry的配置模型
* registry: 这里的registery是DefaultListableBeanFactory
*/
public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
// 1.获取beanDefinitionNames, 这里一共能获取6个
// 5个内置的: internalConfigurationAnnotationProcessor, internalAutowiredAnnotationProcessor, internalCommonAnnotationProcessor,
// internalEventListenerProcessor, internalEventListenerFactory
// 1个注册的主配置类: MainConfig
String[] candidateNames = registry.getBeanDefinitionNames();
for (String beanName : candidateNames) {
// 2.根据beanDefinitionName获取对应的beanDefinition
BeanDefinition beanDef = registry.getBeanDefinition(beanName);
// 3.根据BeanDifinition封装的Class是否是全配置类还是轻量级配置类
// 这里判断依据是BeanDefinition的属性值(存储在父类AttributeAccessorSupport中), 【org.springframework.context.annotation.ConfigurationClassPostProcessor.configurationClass】是否为full或者lite
// 一开始进来的时候, 属性值为null, 这里会跳过
if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) ||
ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) {
if (logger.isDebugEnabled()) {
logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);
}
}
// 4.ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)这个方法会:
// 校验BeanDefinition是否是全配置类还是轻量级d配置类, 这里校验的依据是:
// 如果是AnnotatedGenericBeanDefinition【这里我们的MainConfig对应的bd是这种类型】:
// 全配置类full -- 有@Configuration标签
// 轻量级配置类lite -- 有 @Component @ComponentScan @Import @ImportResource 标签, 或者有@Bean标签的方法
// 这里我们传入的MainConfig对应的bd有 @Configuration标签, 所以属于全配置类, 同时修改BeanDefinition的属性值(存储在父类AttributeAccessorSupport中), 【org.springframework.context.annotation.ConfigurationClassPostProcessor.configurationClass】为full或者lite
else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
// 5.添加到配置类候选列表中
// 【注意】添加到configCandidates中的, 只能是:
// 1.内置的配置类(一般没有)
// 2.ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class) 传入的配置类
// 3.AnnotatedBeanDefinitionReader#register(Config.class) 注册的配置类
configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
}
}
// Return immediately if no @Configuration classes were found
// 6.这里不为空, 跳过
if (configCandidates.isEmpty()) {
return;
}
// Sort by previously determined @Order value, if applicable
// 7.根据@Order标签排序
configCandidates.sort((bd1, bd2) -> {
int i1 = ConfigurationClassUtils.getOrder(bd1.getBeanDefinition());
int i2 = ConfigurationClassUtils.getOrder(bd2.getBeanDefinition());
return Integer.compare(i1, i2);
});
// Detect any custom bean name generation strategy supplied through the enclosing application context
// 8.这里从一级缓存中取org.springframework.context.annotation.internalConfigurationBeanNameGenerator, 取不到
SingletonBeanRegistry sbr = null;
if (registry instanceof SingletonBeanRegistry) {
sbr = (SingletonBeanRegistry) registry;
if (!this.localBeanNameGeneratorSet) {
BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR);
if (generator != null) {
this.componentScanBeanNameGenerator = generator;
this.importBeanNameGenerator = generator;
}
}
}
// 9.environment在创建ApplicationContext对象时已经创建, 这里会跳过
if (this.environment == null) {
this.environment = new StandardEnvironment();
}
// 10解析@Configuration类
ConfigurationClassParser parser = new ConfigurationClassParser(
this.metadataReaderFactory, this.problemReporter, this.environment,
this.resourceLoader, this.componentScanBeanNameGenerator, registry);
Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
do {
// 11.这里是具体的解析@Configuration类的方法, 见@1.2
parser.parse(candidates);
parser.validate();
Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses());
configClasses.removeAll(alreadyParsed);
// Read the model and create bean definitions based on its content
if (this.reader == null) {
this.reader = new ConfigurationClassBeanDefinitionReader(
registry, this.sourceExtractor, this.resourceLoader, this.environment,
this.importBeanNameGenerator, parser.getImportRegistry());
}
this.reader.loadBeanDefinitions(configClasses);
alreadyParsed.addAll(configClasses);
candidates.clear();
if (registry.getBeanDefinitionCount() > candidateNames.length) {
String[] newCandidateNames = registry.getBeanDefinitionNames();
Set<String> oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames));
Set<String> alreadyParsedClasses = new HashSet<>();
for (ConfigurationClass configurationClass : alreadyParsed) {
alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());
}
for (String candidateName : newCandidateNames) {
if (!oldCandidateNames.contains(candidateName)) {
BeanDefinition bd = registry.getBeanDefinition(candidateName);
if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &&
!alreadyParsedClasses.contains(bd.getBeanClassName())) {
candidates.add(new BeanDefinitionHolder(bd, candidateName));
}
}
}
candidateNames = newCandidateNames;
}
}
while (!candidates.isEmpty());
// Register the ImportRegistry as a bean in order to support ImportAware @Configuration classes
if (sbr != null && !sbr.containsSingleton(IMPORT_REGISTRY_BEAN_NAME)) {
sbr.registerSingleton(IMPORT_REGISTRY_BEAN_NAME, parser.getImportRegistry());
}
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();
}
}
1.2 ConfigurationClassParser#parse
public void parse(Set<BeanDefinitionHolder> configCandidates) {
// 1.遍历所有的configCandidates
for (BeanDefinitionHolder holder : configCandidates) {
BeanDefinition bd = holder.getBeanDefinition();
try {
// 2.解析
if (bd instanceof AnnotatedBeanDefinition) {
// 调用的是重载方法 protected final void parse(AnnotationMetadata metadata, String beanName) throws IOException
// 最终调用 protected void processConfigurationClass(ConfigurationClass configClass) throws IOException, 见@1.3
parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());
}
else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {
// 调用的是重载方法 protected final void parse(Class<?> clazz, String beanName) throws IOException
// 最终调用 protected void processConfigurationClass(ConfigurationClass configClass) throws IOException, 见@1.3
parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());
}
else {
// 调用的是重载方法 protected final void parse(@Nullable String className, String beanName) throws IOException
// 最终调用 protected void processConfigurationClass(ConfigurationClass configClass) throws IOException, 见@1.3
parse(bd.getBeanClassName(), holder.getBeanName());
}
}
catch (BeanDefinitionStoreException ex) {
throw ex;
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(
"Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);
}
}
this.deferredImportSelectorHandler.process();
}
1.3 ConfigurationClassParser#processConfigurationClass
protected void processConfigurationClass(ConfigurationClass configClass) throws IOException {
// 1.判断是否需要跳过, 主要是@Condition注解解析, 如果没有@Condition, 则这里会跳过
if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) {
return;
}
// 2.处理Imported, 这里先跳过, 目前 existingClass == null
ConfigurationClass existingClass = this.configurationClasses.get(configClass);
if (existingClass != null) {
if (configClass.isImported()) {
if (existingClass.isImported()) {
existingClass.mergeImportedBy(configClass);
}
// Otherwise ignore new imported config class; existing non-imported class overrides it.
return;
}
else {
// Explicit bean definition found, probably replacing an import.
// Let's remove the old one and go with the new one.
this.configurationClasses.remove(configClass);
this.knownSuperclasses.values().removeIf(configClass::equals);
}
}
// Recursively process the configuration class and its superclass hierarchy.
// 3.将ConfigurationClass封装成SourceClass
SourceClass sourceClass = asSourceClass(configClass);
do {
// 4.循环处理, 直到返回值不为空
// 【注意】doProcessConfigurationClass是解析的核心方法, 见@1.4
sourceClass = doProcessConfigurationClass(configClass, sourceClass);
}
while (sourceClass != null);
// 5.添加到configurationClasses中
this.configurationClasses.put(configClass, configClass);
}
1.4 ConfigurationClassParser#doProcessConfigurationClass
/**
* Apply processing and build a complete {@link ConfigurationClass} by reading the
* annotations, members and methods from the source class. This method can be called
* multiple times as relevant sources are discovered.
* @param configClass the configuration class being build
* @param sourceClass a source class
* @return the superclass, or {@code null} if none found or previously processed
* 翻译: 通过配置类的注解、内部类、方法, 创建一个完整的ConfigurationClass; 这个方法会被循环调用直到发现了相关的源码
*/
@Nullable
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
throws IOException {
// 1.这里并不是有@Component注解才返回true
if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
// 2.循环处理内部类, 一般不会有, 这里先跳过
processMemberClasses(configClass, sourceClass);
}
// Process any @PropertySource annotations
// 3.处理 @PropertySource 注解
for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), PropertySources.class,
org.springframework.context.annotation.PropertySource.class)) {
if (this.environment instanceof ConfigurableEnvironment) {
// 4.解析 @PropertySource 注解, 见@1.5
processPropertySource(propertySource);
}
else {
logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
"]. Reason: Environment must implement ConfigurableEnvironment");
}
}
// Process any @ComponentScan annotations
// 5.处理 @ComponentScan 和 @ComponentScans
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
if (!componentScans.isEmpty() &&
!this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
// 6.遍历解析出来的@ComponentScan标签
for (AnnotationAttributes componentScan : componentScans) {
// The config class is annotated with @ComponentScan -> perform the scan immediately
// 7.调用 ComponentScanAnnotationParser#parse进行扫描 见@1.6
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) {
BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
if (bdCand == null) {
bdCand = holder.getBeanDefinition();
}
// 8.这里的candidate一般是有@Component等标签的, 属于轻量级配置类, 进入
if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
// 9.这里又调用了@1.2的解析方法
parse(bdCand.getBeanClassName(), holder.getBeanName());
}
}
}
}
// Process any @Import annotations
// 10.处理@Import标签, 将@import标签中的类生成bd, 注册到bdMap中
processImports(configClass, sourceClass, getImports(sourceClass), true);
// Process any @ImportResource annotations
// 11.处理@ImportResource标签
AnnotationAttributes importResource =
AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
if (importResource != null) {
String[] resources = importResource.getStringArray("locations");
Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
for (String resource : resources) {
String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
configClass.addImportedResource(resolvedResource, readerClass);
}
}
// Process individual @Bean methods
// 12.处理内置的@Bean方法, 生成对应的bd, 放入bdMap中
Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
for (MethodMetadata methodMetadata : beanMethods) {
configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
}
// Process default methods on interfaces
// 13.处理接口中的默认实现方法
processInterfaces(configClass, sourceClass);
// Process superclass, if any
// 14.处理父类
if (sourceClass.getMetadata().hasSuperClass()) {
String superclass = sourceClass.getMetadata().getSuperClassName();
if (superclass != null && !superclass.startsWith("java") &&
!this.knownSuperclasses.containsKey(superclass)) {
this.knownSuperclasses.put(superclass, configClass);
// Superclass found, return its annotation metadata and recurse
return sourceClass.getSuperClass();
}
}
// No superclass -> processing is complete
return null;
}
1.5 ConfigurationClassParser#processPropertySource
/**
* Process the given <code>@PropertySource</code> annotation metadata.
* @param propertySource metadata for the <code>@PropertySource</code> annotation found
* @throws IOException if loading a property source failed
*/
private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
// 1.获取name属性值, 默认null
String name = propertySource.getString("name");
if (!StringUtils.hasLength(name)) {
name = null;
}
// 2.获取encoding属性值, 默认null
String encoding = propertySource.getString("encoding");
if (!StringUtils.hasLength(encoding)) {
encoding = null;
}
// 3.获取路径
String[] locations = propertySource.getStringArray("value");
Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
// 4.ignoreResourceNotFound, 默认false
boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
Class<? extends PropertySourceFactory> factoryClass = propertySource.getClass("factory");
// 5.获取PropertySourceFactory
PropertySourceFactory factory = (factoryClass == PropertySourceFactory.class ?
DEFAULT_PROPERTY_SOURCE_FACTORY : BeanUtils.instantiateClass(factoryClass));
for (String location : locations) {
try {
// 6.解析路径 classpath:xxx.properties --> classpath:xxx.properties
String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
// 7.根据路径创建Resource --> ClassPathResource【这里路径以classpath开头, 所以生成ClassPathResource】
Resource resource = this.resourceLoader.getResource(resolvedLocation);
// 8.创建PropertySource并添加到 Environment的propertySources属性中
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
}
catch (IllegalArgumentException | FileNotFoundException | UnknownHostException ex) {
// Placeholders not resolvable or resource not found when trying to open it
if (ignoreResourceNotFound) {
if (logger.isInfoEnabled()) {
logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
}
}
else {
throw ex;
}
}
}
}
总结
解析@PropertySource属性, 将其添加到Environment的propertySources属性中
1.6 ComponentScanAnnotationParser#parse
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
// 1.创建BeanDefinitionScanner
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry, componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);
// 2.解析获取@ComponentScan的nameGenerator -- org.springframework.beans.factory.support.BeanNameGenerator
Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
// 3.使用默认的BeanNameGenerator -- AnnotationBeanNameGenerator
boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :
BeanUtils.instantiateClass(generatorClass));
// 4.解析获取@ComponentScan的scopedProxy
ScopedProxyMode scopedProxyMode = componentScan.getEnum("scopedProxy");
if (scopedProxyMode != ScopedProxyMode.DEFAULT) {
scanner.setScopedProxyMode(scopedProxyMode);
}
else {
// 使用 org.springframework.context.annotation.AnnotationScopeMetadataResolver
Class<? extends ScopeMetadataResolver> resolverClass = componentScan.getClass("scopeResolver");
scanner.setScopeMetadataResolver(BeanUtils.instantiateClass(resolverClass));
}
// 5.解析获取@ComponentScan的resourcePattern
scanner.setResourcePattern(componentScan.getString("resourcePattern"));
// 6.解析获取@ComponentScan的includeFilters
for (AnnotationAttributes filter : componentScan.getAnnotationArray("includeFilters")) {
for (TypeFilter typeFilter : typeFiltersFor(filter)) {
scanner.addIncludeFilter(typeFilter);
}
}
// 7.解析获取@ComponentScan的excludeFilters
for (AnnotationAttributes filter : componentScan.getAnnotationArray("excludeFilters")) {
for (TypeFilter typeFilter : typeFiltersFor(filter)) {
scanner.addExcludeFilter(typeFilter);
}
}
// 8.解析获取@ComponentScan的lazyInit
boolean lazyInit = componentScan.getBoolean("lazyInit");
if (lazyInit) {
scanner.getBeanDefinitionDefaults().setLazyInit(true);
}
// 9.解析获取@ComponentScan的basePackages
Set<String> basePackages = new LinkedHashSet<>();
String[] basePackagesArray = componentScan.getStringArray("basePackages");
for (String pkg : basePackagesArray) {
String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg),
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
Collections.addAll(basePackages, tokenized);
}
for (Class<?> clazz : componentScan.getClassArray("basePackageClasses")) {
basePackages.add(ClassUtils.getPackageName(clazz));
}
if (basePackages.isEmpty()) {
basePackages.add(ClassUtils.getPackageName(declaringClass));
}
scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
@Override
protected boolean matchClassName(String className) {
return declaringClass.equals(className);
}
});
// 10.封装完ClassPathBeanDefinitionScanner, 调用其doScan方法开始扫描, 见@1.7
return scanner.doScan(StringUtils.toStringArray(basePackages));
}
总结
1.这个方法主要是根据解析出来的@ComponentScan标签
2.生成@ComponentScan标签扫描的类对应的BeanDefinition, 并注册到beanDefinitionMap中
1.7 ClassPathBeanDefinitionScanner#doScan
/**
* Perform a scan within the specified base packages,
* returning the registered bean definitions.
* <p>This method does <i>not</i> register an annotation config processor
* but rather leaves this up to the caller.
* @param basePackages the packages to check for annotated classes
* @return set of beans registered if any for tooling registration purposes (never {@code null})
* 翻译: 对指定的基础包进行扫描, 返回完成注册的BeanDefinition
*/
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
for (String basePackage : basePackages) {
// 1.内部调用 , 见@1.8, 根据basePackage, 扫描其中的类, 创建为BeanDefinition
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
for (BeanDefinition candidate : candidates) {
// 2.解析@Scope标签
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
// 3.生成beanName
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
if (candidate instanceof AbstractBeanDefinition) {
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
}
// 4.解析其他常用标签, @Lazy @DependsOn @Role @Description等
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
}
// 5.如果beanDefinitionMap中不存在当前beanDefinition
if (checkCandidate(beanName, candidate)) {
// 6.创建BeanDefinitionHolder
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
// 7.根据@Scope标签判断是否需要生成代理bdh, 默认不会
definitionHolder =
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
beanDefinitions.add(definitionHolder);
// 8.将bd注册到beanDefinitionMap中
registerBeanDefinition(definitionHolder, this.registry);
}
}
}
return beanDefinitions;
}
总结
解析@Component标签, 扫描basePackage下面的类, 将有@Component标签的类, 创建对应的BeanDefinition, 注册到beanDefinitionMap中, 同时创建bdh, 并返回
1.8 ClassPathScanningCandidateComponentProvider#scanCandidateComponents
private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
Set<BeanDefinition> candidates = new LinkedHashSet<>();
try {
// 1.解析
// 将 com.tca.spring.framework.learning.ioc.test 转化为
// classpath*:com/tca/spring/framework/learning/ioc/test/**/*.class
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolveBasePackage(basePackage) + '/' + this.resourcePattern;
// 2.根据路径, 找到对应File, 封装成Resource
// 【注意:】这里传入的basePackage下所有的类都会被转化为Resource对象, 包括没有使用@Component、@Controller标签的类
Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);
boolean traceEnabled = logger.isTraceEnabled();
boolean debugEnabled = logger.isDebugEnabled();
for (Resource resource : resources) {
if (traceEnabled) {
logger.trace("Scanning " + resource);
}
if (resource.isReadable()) {
try {
// 3.根据Resource创建对应MetadataReader, 封装了类的元数据(Class信息, 注解等)
MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
// 4.判断当前类是否需要添加到candidate中, 如果类没有使用@Component、@Controller等标签, 则不会被添加到candidate中
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;
}
总结
解析basePackage包, 扫描下面的类, 将有@Component、@Controller等注解的类, 创建对应的BeanDefinition