这个方法主要有两大作用:
1,创建BeanFactory对象
2,解析xml标签并且封装成BeanDefinition对象
1).对传统标签的解析(bean,import等)
2).对自定义的标签解析 如:<context:component-scan base-package="com.xiangxue.jack"/>
①,根据当前解析标签的头信息找到对应的namespaceUri
②,加载spring所有jar中的spring.handlers文件建立映射关系.
③,根据namespaceUri从映射关系中找到对应实现了namespaceHandler借口类.
④,调用实现类的init方法,init方法中注册了各种自定义的标签解析类
⑤,根据
namespaceUri找到对应的解析类,然后调用paser方法完成标签解析
下面是大体流程笔记
一.创建BeanFactory实例工厂,同时设置是否可以循环,是否允许相同名称重新注册不同的bean实现.
二.xml解析前的准备loadBeanDefinitions方法和registerBeanDefinitions方法
(一),loadBeanDefinitions阶段
创建XmlBeanDefinitionReader对象(xml的解析器,将xml委托给这个对象进行解析)
将xml文件 字符串路径转换成Resource对象,获取到这个对象中的文件流使用jdk的sax解析成InputSource
把inputSource,封装成Document文件对象
(二),registerBeanDefinitions阶段
创建BeanDefinitionDocumentReader对象(document的解析器,将document对象的解析委托给这个对象)
从root节点开始进行解析
标签分两类,默认标签解析,自定义标签解析
三,标签解析
(一),默认标签解析,
可以看出import,alias,bean,beans是要进行默认标签解析的
对bean标签属性解析
bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder); 这个方法是解析默认标签中的自定义标签的方法,使用的也是自定义的标签解析方法
如:<bean class="com.xx" p:username="阿三"/>
将beanDefinition 注册到beanFactory中
(二)自定义标签解析 (以 <context:component-scan base-package="com.zqq.po"></context:component-scan> 为例)
根据当前解析标签的头信息找到对应的namespaceUri
加载spring所有jar中的spring.handlers文件建立映射关系.
根据namespaceUri从映射关系中找到对应实现了namespaceHandler借口类.
调用实现类的init方法,init方法中注册了各种自定义的标签解析类
根据
namespaceUri找到对应的解析类,然后调用paser方法完成标签解析
GenericBeanDefinition的属性
metadata = {AnnotationMetadataReadingVisitor@2112}
parentName = null
beanClass = "com.demo.Blue" // bean的类型
scope = "singleton" //bean的作用范围
abstractFlag = false //是否是抽象类
lazyInit = false //是否延迟加载
autowireMode = 0 //自动注入模式
dependencyCheck = 0 //依赖检查
dependsOn = null //依赖对象:先初始化dependsOn的bean
autowireCandidate = true //若为false时,则容器在装配时不会装配这个bean,这个bean可以装配其他bean
primary = false //自动装配出现多个bean时,这个bean作为首选
qualifiers = {LinkedHashMap@2115} size = 0
instanceSupplier = null
nonPublicAccessAllowed = true //允许访问非公开的构造器和方法
lenientConstructorResolution = true //是否以宽松的模式解析构造函数
factoryBeanName = null //指定工厂类和工厂方法
factoryMethodName = null
constructorArgumentValues = null //记录构造器注入属性
propertyValues = {MutablePropertyValues@2116} "PropertyValues: length=0" //普通属性集合
methodOverrides = {MethodOverrides@2117} //方法重写的持有者
initMethodName = null //初始化方法 ,必须是无惨的
destroyMethodName = null //bean的销毁方法,必须是无惨的 只适用单例bean中
enforceInitMethod = false //是否执行初始化方法
enforceDestroyMethod = false //是否执行销毁方法
synthetic = false //是不是用户定义的而不是应用程序本身定义的,创建AOP是为true
role = 0
description = null //bean 的描述
resource = {FileSystemResource@2118} "file [E:\project\IdeaWeb\springDemo\target\classes\com\demo\Blue.class]"
source = {FileSystemResource@2118} "file [E:\project\IdeaWeb\springDemo\target\classes\com\demo\Blue.class]"
attributes = {LinkedHashMap@2119} size = 1