相关文章
Spring IOC系列学习笔记一:前置刷新
Spring IOC系列学习笔记二:obtainFreshBeanFactory方法
Spring IOC系列学习笔记三:parseDefaultElement详解
Spring IOC系列学习笔记四:parseCustomElement解析
Spring IOC系列学习笔记五:context:component-scan 节点解析
Spring IOC系列学习笔记六:invokeBeanFactoryPostProcessors解析
Spring IOC系列学习笔记七:registerBeanPostProcessors
Spring IOC系列学习笔记八:finishBeanFactoryInitialization
Spring IOC系列学习笔记九:getBean方法
Spring IOC系列学习笔记十:createBean方法(上)
Spring IOC系列学习笔记十一:createBean方法(下)
Spring IOC系列学习笔记十二:@Autowire注解
文章目录
前言
已经把refresh方法的前置刷新环境准备了解了一下,现在正式进入refresh方法。
obtainFreshBeanFactory方法概述
从名字中可以看出她会获得一个bean的实例,在这个方法中他去读取spring的配置文件,解析所有的标签,将所有的Bean实例化成BeanDefinition加载到BeanFactory中。
有三个重要的缓存:
- beanDefinitionNames缓存:所有被加载到 BeanFactory 中的 bean 的 beanName 集合。
- beanDefinitionMap缓存:所有被加载到 BeanFactory 中的 bean 的 beanName 和BeanDefinition 映射。
- aliasMap缓存:所有被加载到 BeanFactory 中的 bean 的 beanName 和别名映射。
下面进入正文。
代码块一:obtainFreshBeanFactory
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
1、刷新Bean的工厂
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}
1、刷新bean工厂 见代码块二
代码块二:refreshBeanFactory
@Override
protected final void refreshBeanFactory() throws BeansException {
如果原来已经有bean工厂了就先销毁,创建一个新的工厂
if (hasBeanFactory()) {
destroyBeans();
closeBeanFactory();
}
try {
1、创建bean工厂 ,这儿用的是子实现DefaultListableBeanFactory里面有两个重要的缓存
DefaultListableBeanFactory beanFactory = createBeanFactory();
beanFactory.setSerializationId(getId());
customizeBeanFactory(beanFactory);
2、加载bean定义
loadBeanDefinitions(beanFactory);
synchronized (this.beanFactoryMonitor) {
this.beanFactory = beanFactory;
}
}
catch (IOException ex) {
throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
}
}
2、加载bean定义见代码三,由AbstractXmlApplicationContext实现
代码块三:loadBeanDefinitions
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
1、创建一个xml的读取器,读取BeanFactory
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
2、设置环境
beanDefinitionReader.setEnvironment(this.getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
initBeanDefinitionReader(beanDefinitionReader);
3、加载bean定义
loadBeanDefinitions(beanDefinitionReader);
}
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
1、从配置的资源获取
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
2、从配置中获取要解析的配置文件地址
String[] configLocations = getConfigLocations();
if (configLocations != null) {
3、读取配置文件的bean定义
reader.loadBeanDefinitions(configLocations);
}
}
2、这儿是从web.xml中配置的路径,如果没有配置则读取默认的路径:/WEB-INF/applicationContext.xml
@Override
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
Assert.notNull(locations, "Location array must not be null");
int counter = 0;
for (String location : locations) {
1、读取配置文件返回读取的个数
counter += loadBeanDefinitions(location);
}
return counter;
}
1、读取配置文件返回读取的数量见代码块四
代码块四:loadBeanDefinitions
public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
1、获取资源加载器这儿是ClassPathXmlApplicationContext
ResourceLoader resourceLoader = getResourceLoader();
if (resourceLoader == null) {
throw new BeanDefinitionStoreException(
"Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
}
判断ClassPathXmlApplicationContext是否是ResourcePatternResolver的实例,观察继承图谱为true
if (resourceLoader instanceof ResourcePatternResolver) {
// Resource pattern matching available.
try {
2、将applicationContext.xml封装成 resources
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
3、加载配置文件 返回当前加载的个数
int loadCount = loadBeanDefinitions(resources);
if (actualResources != null) {
for (Resource resource : resources) {
actualResources.add(resource);
}
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
}
return loadCount;
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"Could not resolve bean definition resource pattern [" + location + "]", ex);
}
}
else {
// Can only load single resources by absolute URL.
4、每次只能加载一个资源
Resource resource = resourceLoader.getResource(location);
5、加载配置文件
int loadCount = loadBeanDefinitions(resource);
if (actualResources != null) {
actualResources.add(resource);
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
}
return loadCount;
}
}
3、加载配置文件 见代码块五
代码块五:loadBeanDefinitions
@Override
public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
Assert.notNull(resources, "Resource array must not be null");
int counter = 0;
for (Resource resource : resources) {
1、返回本次加载的个数并加上历史加载的 ,得到的是总个数
counter += loadBeanDefinitions(resource);
}
return counter;
}
1、这儿的方法是XmlBeanDefinitionReader中的loadBeanDefinitions
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
Assert.notNull(encodedResource, "EncodedResource must not be null");
if (logger.isInfoEnabled()) {
logger.info("Loading XML bean definitions from " + encodedResource.getResource());
}
1、获取当前正在加载的资源
Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();
if (currentResources == null) {
currentResources = new HashSet<>(4);
2、把currentResources设置成当前线程解析的资源
this.resourcesCurrentlyBeingLoaded.set(currentResources);
}
3、如果把要加载的资源没有添加进去,则出现了循环加载配置文件抛出异常
if (!currentResources.add(encodedResource)) {
throw new BeanDefinitionStoreException(
"Detected cyclic loading of " + encodedResource + " - check your import definitions!");
}
try {
4、获取inputStream
InputStream inputStream = encodedResource.getResource().getInputStream();
try {
5、把inputStream封装成InputSource
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
5、真正执行的方法 以do开头
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {
inputStream.close();
}
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(
"IOException parsing XML document from " + encodedResource.getResource(), ex);
}
finally {
currentResources.remove(encodedResource);
if (currentResources.isEmpty()) {
this.resourcesCurrentlyBeingLoaded.remove();
}
}
}
5、真正执行的方法 以do开头 见代码块六
代码块六:doLoadBeanDefinitions
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {
1、封装成Document来进行解析(这儿不多做解释,这是一种解析xml的方式还有其他的方式)
Document doc = doLoadDocument(inputSource, resource);
2、注册bean定义,开始解析节点
return registerBeanDefinitions(doc, resource);
}
catch (BeanDefinitionStoreException ex) {
throw ex;
}
catch (SAXParseException ex) {
throw new XmlBeanDefinitionStoreException(resource.getDescription(),
"Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);
}
catch (SAXException ex) {
throw new XmlBeanDefinitionStoreException(resource.getDescription(),
"XML document from " + resource + " is invalid", ex);
}
catch (ParserConfigurationException ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"Parser configuration exception parsing XML from " + resource, ex);
}
catch (IOException ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"IOException parsing XML document from " + resource, ex);
}
catch (Throwable ex) {
throw new BeanDefinitionStoreException(resource.getDescription(),
"Unexpected exception parsing XML document from " + resource, ex);
}
}
代码块七:registerBeanDefinitions
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
1、创建document读取器
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
2、获取原来注册的实例个数
int countBefore = getRegistry().getBeanDefinitionCount();
3、createReaderContext根据resource获取XmlReaderContext
4、registerBeanDefinitions 注册成BeanDefinition实例
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
5、获取新注册的实例个数
return getRegistry().getBeanDefinitionCount() - countBefore;
}
3、createReaderContext根据resource获取XmlReaderContext 见代码块八
4、registerBeanDefinitions 注册成BeanDefinition实例见代码块九
代码块八:createReaderContext
public XmlReaderContext createReaderContext(Resource resource) {
return new XmlReaderContext(resource, this.problemReporter, this.eventListener,
this.sourceExtractor, this, getNamespaceHandlerResolver());
}
public NamespaceHandlerResolver getNamespaceHandlerResolver() {
if (this.namespaceHandlerResolver == null) {
this.namespaceHandlerResolver = createDefaultNamespaceHandlerResolver();
}
return this.namespaceHandlerResolver;
}
protected NamespaceHandlerResolver createDefaultNamespaceHandlerResolver() {
ClassLoader cl = (getResourceLoader() != null ? getResourceLoader().getClassLoader() : getBeanClassLoader());
return new DefaultNamespaceHandlerResolver(cl);
}
public DefaultNamespaceHandlerResolver(@Nullable ClassLoader classLoader) {
this(classLoader, DEFAULT_HANDLER_MAPPINGS_LOCATION);
}
public DefaultNamespaceHandlerResolver(@Nullable ClassLoader classLoader, String handlerMappingsLocation) {
1、这儿handlerMappingsLocation的值为 META-INF/spring.handlers
Assert.notNull(handlerMappingsLocation, "Handler mappings location must not be null");
this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
this.handlerMappingsLocation = handlerMappingsLocation;
}
handlerMappingsLocation的值为:META-INF/spring.handlers ,并且这边有个重要的属性 handlerMappings,handlerMappings 用于存放命名空间和该命名空间handler类的映射。此时handlerMappings内容如下所示:
代码块九:registerBeanDefinitions
protected void doRegisterBeanDefinitions(Element root) {
// Any nested <beans> elements will cause recursion in this method. In
// order to propagate and preserve <beans> default-* attributes correctly,
// keep track of the current (parent) delegate, which may be null. Create
// the new (child) delegate with a reference to the parent for fallback purposes,
// then ultimately reset this.delegate back to its original (parent) reference.
// this behavior emulates a stack of delegates without actually necessitating one.
BeanDefinitionParserDelegate parent = this.delegate;
1、创建BeanDefinitionParserDelegate
this.delegate = createDelegate(getReaderContext(), root, parent);
2、该节点是否是默认的命名空间下的节点
if (this.delegate.isDefaultNamespace(root)) {
3、解析profile属性 就是我们常用的环境比如 dev环境pro环境test环境等 不同环境下配置可能不同
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
4、校验当前节点是否是 当前环境下的 如果不是则直接返回 不做解析
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
if (logger.isInfoEnabled()) {
logger.info("Skipped XML bean definition file due to specified profiles [" + profileSpec +
"] not matching: " + getReaderContext().getResource());
}
return;
}
}
}
5、前置处理
preProcessXml(root);
6、解析节点
parseBeanDefinitions(root, this.delegate);
7、后置处理
postProcessXml(root);
this.delegate = parent;
}
5、解析节点 进入代码十
代码块十:parseBeanDefinitions
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
1、当前节点是否是默认命名空间 最常见的如<beans>节点
if (delegate.isDefaultNamespace(root)) {
2、遍历子节点进行处理
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
if (delegate.isDefaultNamespace(ele)) {
3、处理默认的节点 如bean节点
parseDefaultElement(ele, delegate);
}
else {
4、定制节点 比如<context:component-scan base-package="com.zgf"> </context:component-scan>
delegate.parseCustomElement(ele);
}
}
}
}
5、如果不是则直接走定制的命名空间解析
else {
delegate.parseCustomElement(root);
}
}
最终进入解析节点的过程。
- 如果是默认的节点就进入parseDefaultElement方法,如最常见的bean节点的解析
- 否则进入parseCustomElement方法解析,如常见的开启注解读取<context:component-scan
base-package=“com.zgf”> </context:component-scan>以及。
总结
本文主要介绍了:
- 创建一个新的 BeanFactory:DefaultListableBeanFactory。
- 根据 web.xml 中 contextConfigLocation 配置的路径,读取 Spring
配置文件,验证配置文件的内容,并封装成 Resource。 - 根据 Resource 加载 XML 配置文件,并解析成 Document 对象 。
- 拿到 Document 中的根节点,遍历根节点和所有子节点。