当使用到 context:annotation-config/ 或者context:component-scan/配置的时候,及声明做为其它BeanFactoryPostProcessor的时候。
本例使用:
<context:component-scan base-package&#61;"com.songbl.config" >context:component-scan>
容器启动&#xff0c;在解析配置文件的时候&#xff1a;
DefaultBeanDefinitionDocumentReader.java
->
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
if (delegate.isDefaultNamespace(root)) {
NodeList nl &#61; root.getChildNodes();
for (int i &#61; 0; i < nl.getLength(); i&#43;&#43;) {
Node node &#61; nl.item(i);
if (node instanceof Element) {
Element ele &#61; (Element) node;
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate);
}
else {
//调用其它自定义解析器。
//根据命名空间获取处理器&#xff0c;在处理器中根据属性标签&#xff0c;获取解析器&#xff0c;解析
delegate.parseCustomElement(ele);
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
//&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
//&#61;>
public BeanDefinition parseCustomElement(Element ele, &#64;Nullable BeanDefinition containingBd) {
// 获取对应的命名空间
String namespaceUri &#61; getNamespaceURI(ele);
if (namespaceUri &#61;&#61; null) {
return null;
}
// 根据命名空间找到对应的NamespaceHandler 接口->对象&#xff0c;
// 根据命名空间uri&#xff0c;找到value &#xff0c;再实例化处理器。处理器包含解析器&#xff0c;根据ele找到解析器
//内部有实例化的调用&#xff0c;实例化NamespaceHander
NamespaceHandler handler &#61; this.readerContext.getNamespaceHandlerResolver().resolve(namespaceUri);
if (handler &#61;&#61; null) {
error("Unable to locate Spring NamespaceHandler for XML schema namespace [" &#43; namespaceUri &#43; "]", ele);
return null;
}//举例使用ContextNamespaceHandler ,map里面有很多解析器
// 调用自定义的NamespaceHandler进行解析。通过findParserForElement&#xff0c;找到对应的解析器
//parse 是根据属性标签找到的
return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd));
}
//&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
// hander 实例化时候会注册一些解析器
//注册各种解析器&#xff0c;其中有ComponentScanBeanDefinitionParser
ContextNamespaceHandler.java
&#64;Override
public void init() {
registerBeanDefinitionParser("property-placeholder", new PropertyPlaceholderBeanDefinitionParser());
registerBeanDefinitionParser("property-override", new PropertyOverrideBeanDefinitionParser());
registerBeanDefinitionParser("annotation-config", new AnnotationConfigBeanDefinitionParser());
//&#61;&#61;&#61;&#61;&#61;用到扫描parse,就回加上InterXXconfigXXX&#61;&#61;&#61;&#61;&#61;
registerBeanDefinitionParser("component-scan", new ComponentScanBeanDefinitionParser());
registerBeanDefinitionParser("load-time-weaver", new LoadTimeWeaverBeanDefinitionParser());
registerBeanDefinitionParser("spring-configured", new SpringConfiguredBeanDefinitionParser());
....
}
spring.handlers&#xff1a;
http\://www.springframework.org/schema/context&#61;org.springframework.context.config.ContextNamespaceHandler
根据指定的xml&#xff0c;实例化对应的Hander&#xff0c;在实例化Hander的同时&#xff0c;也注册了解析器。假如application.xml文件是&#xff1a;
接上面的parse方法
ComponentScanBeanDefinitionParser.java
public BeanDefinition parse(Element element, ParserContext parserContext) {
// 获取
String basePackage &#61; element.getAttribute(BASE_PACKAGE_ATTRIBUTE);
// 解析占位符
basePackage &#61; parserContext.getReaderContext().getEnvironment().resolvePlaceholders(basePackage);
// 解析base-package
String[] basePackages &#61; StringUtils.tokenizeToStringArray(basePackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
// 构建和配置ClassPathBeanDefinitionScanner
ClassPathBeanDefinitionScanner scanner &#61; configureScanner(parserContext, element);
// 使用scanner在执行的basePackages包中执行扫描&#xff0c;返回已注册的bean定义
Set<BeanDefinitionHolder> beanDefinitions &#61; scanner.doScan(basePackages);
// 组件注册&#xff08;包括注册一些内部的注解后置处理器&#xff0c;触发注册事件&#xff09;。
//扫描&#xff0c;下面有internalXXConfigurationXX
registerComponents(parserContext.getReaderContext(), beanDefinitions, element);
return null;
}
使用scanner扫描指定包下的类&#xff0c;拿到bean信息后进行注册。返回已注册的bean信息用于注册组件。
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
Set<BeanDefinitionHolder> beanDefinitions &#61; new LinkedHashSet<>();
// 遍历basePackages
for (String basePackage : basePackages) {
// 扫描basePackage,将符合要求的bean定义全部找出来
Set<BeanDefinition> candidates &#61; findCandidateComponents(basePackage);
// 遍历所有候选的bean定义
for (BeanDefinition candidate : candidates) {
// 解析&#64;Scope注解&#xff0c;包括scopeName和proxyMode
ScopeMetadata scopeMetadata &#61; this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
// 使用beanName生成器来生成beanName
String beanName &#61; this.beanNameGenerator.generateBeanName(candidate, this.registry);
if (candidate instanceof AbstractBeanDefinition) {
// 处理beanDefinition对象&#xff0c;例如&#xff0c;此bean是否可以自动装配到其他bean中
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
}
if (candidate instanceof AnnotatedBeanDefinition) {
// 处理定义在目标类上的通用注解&#xff0c;包括&#64;Lazy&#xff0c;&#64;Primary&#xff0c;&#64;DependsOn&#xff0c;&#64;Role&#xff0c;&#64;Description
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
}
// 检查beanName是否已经注册过&#xff0c;如果注册过&#xff0c;检查是否兼容
if (checkCandidate(beanName, candidate)) {
// 将当前遍历bean的bean定义和beanName封装成BeanDefinitionHolder
BeanDefinitionHolder definitionHolder &#61; new BeanDefinitionHolder(candidate, beanName);
// 根据proxyMode的值&#xff0c;选择是否创建作用域代理
definitionHolder &#61;
AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
beanDefinitions.add(definitionHolder);
// &#61;&#61;&#61;&#61;&#61;&#61;&#61;注册beanDefinition&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
registerBeanDefinition(definitionHolder, this.registry);
}
}
}
return beanDefinitions;
}
其中有一个重要的configurationclasspostprocessor的beanName
protected void registerComponents(
XmlReaderContext readerContext, Set<BeanDefinitionHolder> beanDefinitions, Element element) {
Object source &#61; readerContext.extractSource(element);
// 使用注解的tagName和source构建CompositeComponentDefinition
CompositeComponentDefinition compositeDef &#61; new CompositeComponentDefinition(element.getTagName(), source);
// 将扫描到的所有beanDefinition添加到compositeDef的nestedComponents属性中
for (BeanDefinitionHolder beanDefHolder : beanDefinitions) {
compositeDef.addNestedComponent(new BeanComponentDefinition(beanDefHolder));
}
// Register annotation config processors, if necessary.
boolean annotationConfig &#61; true;
if (element.hasAttribute(ANNOTATION_CONFIG_ATTRIBUTE)) {
// 获取component-scan标签的annotation-config属性值&#xff0c;默认为true
annotationConfig &#61; Boolean.parseBoolean(element.getAttribute(ANNOTATION_CONFIG_ATTRIBUTE));
}
if (annotationConfig) {
// 如果annotation-config属性值为true&#xff0c;在给定的注册表中注册所有用于注解的bean后置处理器
Set<BeanDefinitionHolder> processorDefinitions &#61;
AnnotationConfigUtils.registerAnnotationConfigProcessors(readerContext.getRegistry(), source);
for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
// *******其中有一个重要的configurationclasspostprocessor******
// 将注册的注解后置处理器的BeanDefinition添加到compositeDef的nestedComponents属性中
compositeDef.addNestedComponent(new BeanComponentDefinition(processorDefinition));
}
}
// 触发组件注册事件&#xff0c;默认实现为EmptyReaderEventListener
readerContext.fireComponentRegistered(compositeDef);
}
//&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;&#61;
registerAnnotationConfigProcessors(.....){
.......
// 注册内部管理的用于处理&#64;configuration注解的后置处理器的bean
if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def &#61; new RootBeanDefinition(ConfigurationClassPostProcessor.class);
def.setSource(source);
// 注册BeanDefinition到注册表中&#xff0c;registerBeanPostProcessors(beanFactory)的时候实例化并调用。ConfigurationClassPostProcessor到注册表中&#xff08;internalConfigurationAnnotationProcessor&#xff09;
beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
}
}
在读取配置文件的时候&#xff0c;解析&#xff0c;遇见 context:component-scan&#xff0c;实例化Hander&#xff0c;同时注册各种解析器&#xff0c;调用parse()&#xff0c;开始使用对应的解析器开始解析&#xff0c;componentScanBeanDefinitionParse-parse()&#xff0c;各种解析&#xff0c;最后回注册ConfigurationClassPostProcessor&#xff0c;待到
refresh()->
registerBeanPostProcessors(beanFactory);实例化、调用