之前在学习关于BeanDefinition的载入的时候,在书上看到了这样的两段代码。
org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(XmlBeanDefinitionReader)
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}
以及
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(Resource...)
public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
Assert.notNull(resources, "Resource array must not be null");
int counter = 0;
for (Resource resource : resources) {
counter += loadBeanDefinitions(resource);
}
return counter;
}
但是在AbstractXmlApplicationContext类中关于直接获取Resource的方法是这样描述的
protected Resource[] getConfigResources() {
return null;
}
这在我当初的学习过程中出现了很大的问题。很显然我们不能将一个空的Resource数组传入loadDefinitions方法中。
实际上这样设计是为了让我们通过configLocation来确定Resource,并实现Resource的加载。
我们来一步一步看
进入到org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(String...)
我们发现这是重写了父类的方法
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);
}
接下来就是整个方法的核心,我们看这个方法调用了一个方法
public int loadBeanDefinitions(String location, Set<Resource> actualResources) throws BeanDefinitionStoreException {
ResourceLoader resourceLoader = getResourceLoader();
if (resourceLoader == null) {
throw new BeanDefinitionStoreException(
"Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
}
if (resourceLoader instanceof ResourcePatternResolver) {
// Resource pattern matching available.
try {
Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
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);
int loadCount = loadBeanDefinitions(resource);
if (actualResources != null) {
actualResources.add(resource);
}
if (logger.isDebugEnabled()) {
logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
}
return loadCount;
}
}
这里面有一个方法
int loadCount = loadBeanDefinitions(resources);
进入到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) {
counter += loadBeanDefinitions(resource);
}
return counter;
}
进入到这里面的loadBeanDefinitions(resource)方法
这是一个抽象方法,交由子类进行实现
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
return loadBeanDefinitions(new EncodedResource(resource));
}
现在就和最开始的那个地方对应上了,整个过程还是比较复杂的。所以建议还是通过代码跟踪的方式一步步去看整个实现的流程。看书固然是一件好事,但是也要学会个人的思考。