Spring配置文件的解析是通过BeanDefinitionReader来实现的,其实了解BeanDefinitionReader实现的机制就会发现,其只是将ApplicationContext.xml配置文件解析成Document对象,真正对xml中元素解析的类是在BeanDefinitionDocumentReader的实现类中来完成的,接下来我们也会对它进行介绍。

Spring源码学习--Spring配置文件解析BeanDefinitionReader(二)_BeanDefinitionReader

对ApplicationContext.xml中开始解析的方法是loadBeanDefinitions

AbstractBeanDefinitionReader的loadBeanDefinitions中会对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) {
counter += loadBeanDefinitions(location);
}
return counter;
}
@Override
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
return loadBeanDefinitions(location, null);
}

loadBeanDefinitions中会根据文件地址来进行解析操作:


public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
//判断资源地址类型
if (resourceLoader instanceof ResourcePatternResolver) {
// Resource pattern matching available.
try {
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
//在子类的loadBeanDefinitions进行解析操作
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.
Resource resource = resourceLoader.getResource(location);
//在子类的loadBeanDefinitions进行解析操作
int loadCount = loadBeanDefinitions(resource);
if (actualResources != null) {
actualResources.add(resource);
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
}
return loadCount;
}
}

loadBeanDefinitions是在子类XmlBeanDefinitionReader中实现


@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}

doLoadBeanDefinitions会将文件地址解析为数据流,然后解析数据流

public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {

..........

try {
//从xml文件中获取流
InputStream inputStream = encodedResource.getResource().getInputStream();
try {
InputSource inputSource = new InputSource(inputStream);
if (encodedResource.getEncoding() != null) {
inputSource.setEncoding(encodedResource.getEncoding());
}
//解析文件流
return doLoadBeanDefinitions(inputSource, encodedResource.getResource());
}
finally {
inputStream.close();
}
}
.......
}

doLoadBeanDefinitions中会将文件流解析成Document对象


protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
try {
Document doc = doLoadDocument(inputSource, resource);
return registerBeanDefinitions(doc, resource);
}
.....
}

registerBeanDefinitions中会创建BeanDefinitionDocumentReader来对Document进行解析,对Spring配置文件中的元素进行解析处理。


public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
//创建Document解析处理器
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
int countBefore = getRegistry().getBeanDefinitionCount();
//在BeanDefinitionDocumentReader中解析xml中配置的元素
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
return getRegistry().getBeanDefinitionCount() - countBefore;
}


总结:简单来说BeanDefinitionReader所做的处理操作是将配置的ApplicationContext.xml解析成为Document对象,接下来会有

BeanDefinitionDocumentReader来对Document进行解析。