SpringFramework的核心:IOC容器的實現------Bean的Resource定位一些細節補充
之前在學習關於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));
}
現在就和最開始的那個地方對應上了,整個過程還是比較複雜的。所以建議還是通過程式碼跟蹤的方式一步步去看整個實現的流程。看書固然是一件好事,但是也要學會個人的思考。