上文我們講解了refresh方法中的invokeBeanFactoryPostProcessors方法,但是對于其中的
BeanDefinitionRegistryProcessor
的實作類的方法還沒有提及,本文将着重講解Spring framework中對于該接口唯一的也是最為重要的實作類
ConfigurationClassPostProcessor
的
processConfigBeanDefinitions
方法。
ConfigurationClassPostProcessor
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
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);
}
//上面兩個if分别是判斷目前的beanFactoryPostProcessor方法是否已經執行過了
this.registriesPostProcessed.add(registryId);
processConfigBeanDefinitions(registry);
}
在上文中已經分析過對于接口
BeanDefinitionRegistryPostProcessor
和接口
BeanFactoryPostProcessor
的兩個方法調用的時機是不同的,是以每次調用
BeanDefinitionRegistryPostProcessor
的方法時都會将生成的
hashcode
存儲在集合中,防止被重複調用。下面就進入了方法的主要邏輯:
public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
//擷取AnnotationConfigApplicationContext中内置的bean(其中一個是AppConfig,其他5個才是真正spring的)
String[] candidateNames = registry.getBeanDefinitionNames();
for (String beanName : candidateNames) {
BeanDefinition beanDef = registry.getBeanDefinition(beanName);
//判斷是否被解析過
//full lite屬性分别對應加了@Configuration的類和沒有加@Configuration的類
//isFullConfigurationClass(beanDef) 全配置類
if (ConfigurationClassUtils.isFullConfigurationClass(beanDef) ||
ConfigurationClassUtils.isLiteConfigurationClass(beanDef)) {
//進入這個if語句塊說明已經被解析過了,往BeanDefinition的attributes中put了一個key,value值
//beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);全配置類的key-value值
if (logger.isDebugEnabled()) {
logger.debug("Bean definition has already been processed as a configuration class: " + beanDef);
}
}
//加了@Configuration的類會設定為full屬性(後面會通過cglib生成代理對象)
//判斷一個類是不是配置類
else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
//如果加了以上注解就将BeanDefinitionHolder放入到BeanDefinitionHolder的集合中去
//因為加了以上注解就可以認為還需要進行解析 還有新的類需要注冊到beanFactory中去
configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
}
}
// Return immediately if no @Configuration classes were found
if (configCandidates.isEmpty()) {
return;
}
//在上面的checkConfigurationClassCandidate方法中已經擷取了bean的order值(如果加了這個注解的話,并将這個order值存放到了bean的attributes)
// Sort by previously determined @Order value, if applicable
//根據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
SingletonBeanRegistry sbr = null;
if (registry instanceof SingletonBeanRegistry) {
sbr = (SingletonBeanRegistry) registry;
if (!this.localBeanNameGeneratorSet) {
//為beanName生成政策指派,但是這裡調用getSingleton方法從單例池中擷取,肯定是擷取不到的,因為沒有在哪裡将這個bean執行個體化過
BeanNameGenerator generator = (BeanNameGenerator) sbr.getSingleton(CONFIGURATION_BEAN_NAME_GENERATOR);
if (generator != null) {
this.componentScanBeanNameGenerator = generator;
this.importBeanNameGenerator = generator;
}
}
}
if (this.environment == null) {
this.environment = new StandardEnvironment();
}
// Parse each @Configuration class
//執行個體化ConfigurationClassParser用于解析配置類(友善傳參,其實就是封裝了一個資料結構)
ConfigurationClassParser parser = new ConfigurationClassParser(
this.metadataReaderFactory, this.problemReporter, this.environment,
this.resourceLoader, this.componentScanBeanNameGenerator, registry);
//定義了candidates的目的是為了去除重複(使用者傳入的配置類可能有重複),利用了Set集合的特性
//alreadyParsed用于判斷是否處理過
Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
do {
//掃描包
parser.parse(candidates);
parser.validate();
//取出上面parse方法中解析出的所有bean
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());
}
//除了普通類(@Component注解的類)都通過這個方法注冊beanFactory中去,到目前為止掃描出來的bd都已經放到BeanDefinitionMap中去了
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();
}
}
首先擷取所有的容器中的BeanDefinition的name值,在上文中已經分析過了,此時Spring beanFactory容器中除了Spring 自己添加的後置處理器以外,我們項目中自己的bd其實隻有一個,就是在
AnnotationConfigApplicationContext
的構造方法或者調用
register
方法注冊的我們的java-config配置類,在本例中是
AppConfig
。然後進入for循環周遊所有的BeanDefinitionNames的集合,因為其他幾個bd都是Spring内置的都不符合後面的要求是以這裡隻有一個bd,也就是
AppConfig
,
AppConfig
中隻有兩個注解。在
for
循環中首先判斷bd的attributes集合中是否已經包含存在了
full
或
lite
的值。
@ComponentScan("wjc.CircularReferences")
@Configuration
public class AppConfig {
}
關于bd的attribute屬性在BeanDefinition一文中已經詳細解釋過了這裡就不再贅述了,回歸代碼的流程,這裡第一次進入一定不會為true,因為這個類并沒有解析過,自然也不可能有值,接下來會去判斷目前的類是不是一個配置類
public static boolean checkConfigurationClassCandidate(
BeanDefinition beanDef, MetadataReaderFactory metadataReaderFactory) {
String className = beanDef.getBeanClassName();
if (className == null || beanDef.getFactoryMethodName() != null) {
return false;
}
AnnotationMetadata metadata;
if (beanDef instanceof AnnotatedBeanDefinition &&
className.equals(((AnnotatedBeanDefinition) beanDef).getMetadata().getClassName())) {
metadata = ((AnnotatedBeanDefinition) beanDef).getMetadata();
}
else if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) beanDef).hasBeanClass()) {
// Check already loaded Class if present...
// since we possibly can't even load the class file for this Class.
Class<?> beanClass = ((AbstractBeanDefinition) beanDef).getBeanClass();
metadata = new StandardAnnotationMetadata(beanClass, true);
}
else {
try {
MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(className);
metadata = metadataReader.getAnnotationMetadata();
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not find class file for introspecting configuration annotations: " +
className, ex);
}
return false;
}
}
if (isFullConfigurationCandidate(metadata)) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
}
else if (isLiteConfigurationCandidate(metadata)) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
}
else {
return false;
}
// It's a full or lite configuration candidate... Let's determine the order value, if any.
Integer order = getOrder(metadata);
if (order != null) {
beanDef.setAttribute(ORDER_ATTRIBUTE, order);
}
return true;
}
擷取注解上的資訊,然後判斷該類是否聲明了@Configuration注解,Spring在處理是否加了@Configuration注解的類上有不同的處理,這要涉及到
ConfigurationClassPostProcessor
實作
BeanFactoryPostProcessor
接口的方法,等到下文再來講解,如果加了
@Configuration
注解後會向bd的attribute中存放一個鍵值對資料
如果目前的bd所描述的類上沒有聲明
@Configuration
注解。那麼就去判斷目前的bd是否是一個可能存在注解資訊的類,這裡會根據是否存在
@Bean
方式注入其他的bd,或者是否聲明需要解析的注解
public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) {
if (metadata.isInterface()) {
return false;
}
//candidateIndicators集合會在靜态代碼塊中指派
for (String indicator : candidateIndicators) {
if (metadata.isAnnotated(indicator)) {
return true;
}
}
try {
//如果上述的注解都沒有,那麼還會再判斷目前的bd中是否存在@Bean注解的方法,因為這也是代表是否需要解析的一個屬性
return metadata.hasAnnotatedMethods(Bean.class.getName());
}
catch (Throwable ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to introspect @Bean methods on class [" + metadata.getClassName() + "]: " + ex);
}
return false;
}
}
滿足上述判斷要求的bd就會被加入方法最開始建立的
configCandidates
集合,
for
循環周遊完成後會去判斷集合是否為空,若為空則說明不存在需要解析的配置類,就直接傳回,否則會先調用排序方法對bd進行排序,排序的原則就是根據bd的
Order
值,這個值會根據
Order
注解注入(如果有的話)
接下來就到了掃描包的方法
protected void processConfigurationClass(ConfigurationClass configClass) throws IOException {
if (this.conditionEvaluator.shouldSkip(configClass.getMetadata(), ConfigurationPhase.PARSE_CONFIGURATION)) {
return;
}
//處理Imported的情況,就是判斷目前這個注解類有沒有被别的類import
//configurationClasses掃描出來的bean會放到這個集合中(這個集合中的bean還沒有放入到容器中)
ConfigurationClass existingClass = this.configurationClasses.get(configClass);
if (existingClass != null) {
//處理Imported的情況 判斷目前類是否已經被處理過了(因為目前類可能被别的類通過@Import注解已經注冊處理了)
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.
//進行類型轉換,封裝了parser解析器,注解資訊,class資訊
//sourceClass - AppConfig.class 這裡是為了擷取目前BeanDefinition的類資訊
SourceClass sourceClass = asSourceClass(configClass); //configClass中封裝了beanName,該資源的路徑、注解資訊等資料
do {
sourceClass = doProcessConfigurationClass(configClass, sourceClass);
}
while (sourceClass != null);
//普通類(加@Component的類)在掃描出來的時候就被注冊進了BeanDefinitionMap中去,而ImportSelector、ImportBeanDefinitionRegistrar、@Bean都會在parser.parse方法結束後
//去調用loadBeanDefinition的方法來處理這些類
this.configurationClasses.put(configClass, configClass);
}
這裡有人可能會奇怪明明我們傳入了
AppConfig
類需要解析,為什麼Spring上來就要先判斷目前的類是不是被别的類通過
Import
注解注入的,其實試想一下我們掃描出來的類有可能也是一個配置類,或者帶有配置資訊又導入了其它的類,這裡就涉及到遞歸掃描、解析了,是以才會方法開始就判斷目前的bd是否由其他的bd注入的。
protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
throws IOException {
//傳入的參數configClass一定是加了ComponentScan,Component,Configuration。。等注解的類,否則前面就不會将目前的configClass放到集合中
if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
// Recursively process any member (nested) classes first
//有内部類的話 處理内部類,裡面調用了sourceClass.getDeclaredClasses() -- 得到該類所有的内部類,除去父類的
processMemberClasses(configClass, sourceClass);
}
// Process any @PropertySource annotations
//處理所有的@PropertySources注解的類
//@PropertySource注解可以在java-config類中導入proerties配置檔案
for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), PropertySources.class,
org.springframework.context.annotation.PropertySource.class)) {
if (this.environment instanceof ConfigurableEnvironment) {
processPropertySource(propertySource);
}
else {
logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
"]. Reason: Environment must implement ConfigurableEnvironment");
}
}
// Process any @ComponentScan annotations
//擷取ComponentScan注解中的所有資訊(ComponentScans可以包含多個ComponentScan,一般很少使用)
//ComponentScan中除了配置value(掃描包的路徑)以外還可以配置很多屬性
//比如includeFilters、excludeFilters等
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
if (!componentScans.isEmpty() &&
!this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
//ComponentScan注解中的value是一個數組 是以這裡要for循環
//這個for循環先掃描ComponentScan注解下包路徑的所有類,存放到集合中
for (AnnotationAttributes componentScan : componentScans) {
// The config class is annotated with @ComponentScan -> perform the scan immediately
//解析、掃描包下我們定義的交給spring管理的類
//這裡掃描出來了所有@Component、@Service。。等注解的類
//掃描出來以後同時注冊到beanFactory中,是以以下這行parse方法執行完以後就往beanFactory中加入了掃描路徑下的bean,但是bean中的注解資訊并沒有解析,比如import..
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
//檢查掃描出來的類當中是否還含有configuration
//這個for循環是對外層for循環掃描的類進行解析判斷是否還有配置類
for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
//判斷bean是不是BeanDefinitionResource
//BeanDefinitionResource和BeanDefinitionHolder擷取BeanDefinition的方法不一樣
BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
if (bdCand == null) {
bdCand = holder.getBeanDefinition();
}
//以上這幾行代碼就是為了擷取BeanDefinition
/*
* 判斷是否加了以下注解
* candidateIndicators.add(Component.class.getName());
candidateIndicators.add(ComponentScan.class.getName());
candidateIndicators.add(Import.class.getName());
candidateIndicators.add(ImportResource.class.getName());
metadata.hasAnnotatedMethods(Bean.class.getName())
* */
if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
//如果componentScan下掃描出來的類中還有配置類再次去解析配置類并掃描注冊到beanFactory中
//從這裡進入processImports方法其實是處理掃描出來的通過Import注解注入的類
parse(bdCand.getBeanClassName(), holder.getBeanName());
}
}
}
}
/*
* 上面的代碼就是解析配置類中的ComponentScan注解然後去掃描包下普通類 @Component 并注冊到map中去
* 并且以上代碼将basePackages下所有的類 進行判斷是否加了Import注解 如果加了Import注解已經将其解析完畢了
* */
// Process any @Import annotations
//解析配置類中的@Import注解
//Import可以傳入三種類
//1.普通的bean
//2.ImportBeanDefinitionRegistrar(spring的擴充點之一) ---- 實作這個類以後可以擷取到DefaultListableBeanFactory中的存放bean的Map
// 我們可以動态得修改這個Map,可以參與将一個類變成BeanDefinition的過程
// 往bean工廠的map中添加一個bd有三種方式1.調用register方法 2.調用scan方法 3.實作以上接口
//3.ImportSelector
/*
* getImports擷取注解Import中的内容
* 從這裡執行processImports方法其實是為了解析AppConfig中的Import注解
* 因為通過AppConfig的ComponentScan注解掃描進來的類上的Import在上面的for循環中調用parse(bdCand.getBeanClassName(), holder.getBeanName());方法已經解析好了
* */
processImports(configClass, sourceClass, getImports(sourceClass), true);
// Process any @ImportResource annotations
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
//解析配置類中的@Bean注解注入的類,但是這裡不會注冊bean 會将BeanMethod放到集合中在外部調用load方式時統一處理@Bean和@Import
Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
for (MethodMetadata methodMetadata : beanMethods) {
configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
}
// Process default methods on interfaces
processInterfaces(configClass, sourceClass);
// Process superclass, if any
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;
}
解析出
@ComponentScan
注解中配置的全路徑後調用
parse
方法進行解析
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
//spring初始化時候的掃描用的是這裡new的scanner而不是執行個體化AnnotationConfigApplicationContext構造方法中執行個體化的那個對象
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);
//擷取beanName的生成器
//問題:我們沒有自定義beanName的生成器什麼時候會給這個生成器賦一個預設值
//ComponentScan注解中nameGenerator有預設值
Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :
BeanUtils.instantiateClass(generatorClass));
//spring mvc會對此進行處理
ScopedProxyMode scopedProxyMode = componentScan.getEnum("scopedProxy");
if (scopedProxyMode != ScopedProxyMode.DEFAULT) {
scanner.setScopedProxyMode(scopedProxyMode);
}
else {
Class<? extends ScopeMetadataResolver> resolverClass = componentScan.getClass("scopeResolver");
scanner.setScopeMetadataResolver(BeanUtils.instantiateClass(resolverClass));
}
scanner.setResourcePattern(componentScan.getString("resourcePattern"));
//周遊過濾器在AnnotationConfigApplicationContext構造方法中執行個體化reader的時候會傳入過濾器的預設值
for (AnnotationAttributes filter : componentScan.getAnnotationArray("includeFilters")) {
for (TypeFilter typeFilter : typeFiltersFor(filter)) {
scanner.addIncludeFilter(typeFilter);
}
}
for (AnnotationAttributes filter : componentScan.getAnnotationArray("excludeFilters")) {
for (TypeFilter typeFilter : typeFiltersFor(filter)) {
scanner.addExcludeFilter(typeFilter);
}
}
//擷取AppConfig類的@ComponentScan注解中是否配置了全局懶加載
boolean lazyInit = componentScan.getBoolean("lazyInit");
//如果設定了懶加載 就将預設設定為true
//相當與設定了一個全局變量(因為這個時候隻解析了AppConfig類還沒有beanDefinition)與xml中的<beans>标簽中配置懶加載的效果一樣
//之後解析的每個bean都會設定這個預設值
if (lazyInit) {
scanner.getBeanDefinitionDefaults().setLazyInit(true);
}
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);
}
});
//掃描類
return scanner.doScan(StringUtils.toStringArray(basePackages));
}
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) {
//掃描basePackage下的java檔案
//并轉化為BeanDefinition類型
//真正做掃描的方法findCandidateComponents
Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
for (BeanDefinition candidate : candidates) {
//解析scope屬性
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
if (candidate instanceof AbstractBeanDefinition) {
//通過scan掃描的bean都是實作了AbstractBeanDefinition的
//因為掃描以後用ScannedGenericBeanDefinition封裝了類,而AbstractBeanDefinition繼承了GenericBeanDefinition
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
//設定預設屬性LazyInit、AutoWireMode等
}
//如果這個類加了注解就将注解上的值設定到bean中去
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
}
//判斷map中是否已經存在目前的bean
if (checkCandidate(beanName, candidate)) {
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
definitionHolder =
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
beanDefinitions.add(definitionHolder);
//将掃描的符合條件的bean添加到factory的map中去
registerBeanDefinition(definitionHolder, this.registry);
}
}
}
return beanDefinitions;
}
mybatis中就是利用了這個
doScan
方法來實作
mapper
接口的掃描
這裡也可以看到在掃描包路徑的時候并不是通過在
AnnotationConfigApplicationContext
中建立的那個
scanner
,而是重新執行個體化了一個,再解析完畢以後會将put到
configurationClasses
集合中的所有bd都去周遊,處理
Import
、
@Bean
、xml方式注入的bd
private void loadBeanDefinitionsForConfigurationClass(
ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {
if (trackedConditionEvaluator.shouldSkip(configClass)) {
String beanName = configClass.getBeanName();
if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
this.registry.removeBeanDefinition(beanName);
}
this.importRegistry.removeImportingClass(configClass.getMetadata().getClassName());
return;
}
//完成普通類的注冊
//因為這裡的普通的類肯定是通過Import注解注入進來的
//因為如果是加了@Component注解的普通類在掃描的時候就完成了注冊 configClass.isImported()會傳回false
//是以判斷目前類隻要存在注入它的類就當作是普通類在這裡解析
if (configClass.isImported()) {
registerBeanDefinitionForImportedConfigurationClass(configClass);
}
//配置類中的@Bean注解中傳回的對象在這裡解析處理
for (BeanMethod beanMethod : configClass.getBeanMethods()) {
loadBeanDefinitionsForBeanMethod(beanMethod);
}
//xml
loadBeanDefinitionsFromImportedResources(configClass.getImportedResources());
//注冊registrar
//在解析Import時,對于實作了BeanDefinitionRegistrar接口的實作類會被放入到importBeanDefinitionRegistrars中
//然後在這裡執行我們自己的BeanDefinitionRegistrar實作類相應的方法
loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
//如果不是以上幾種情況,說明這個類已經注入了(比如某個了加了@Component注解,但是同時又加了@Import注解在之前解析的時候也被會放入到這個集合中。或者隻加了@Component注解的類也會在這個集合中)
//但是這種情況就不會對該bd進行任何處理,因為在beanFactory中其實已經有這個bd存在了
}