【IT168技术文档】
==>AbstractBeanDefinitionReader.loadBeanDefinitions()
public int loadBeanDefinitions(String location) 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 ...{
<span style="color:red;">
//根据配置文件读取相应配置
Resource[] resources = ((ResourcePatternResolver) resourceLoader).
getResources(location);</span>
int loadCount = loadBeanDefinitions(resources);
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 (logger.isDebugEnabled()) ...{
logger.debug("Loaded " + loadCount + " bean definitions from location
[" + location + "]");}
return loadCount;
}
}
这个是其中一个ResourceLoader的实现
==>PathMatchingResourcePatternResolver.getResources(String locationPattern);
public Resource[] getResources(String locationPattern) throws IOException ...{
Assert.notNull(locationPattern, "Location pattern must not be null");
if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) ...{
// a class path resource (multiple resources for same name possible)
<span style="color:red;">
if (getPathMatcher().isPattern(locationPattern.substring(
CLASSPATH_ALL_URL_PREFIX.length()))) ...{// a class path resource pattern
return findPathMatchingResources(locationPattern);
}
else ...{
// all class path resources with the given name
return findAllClassPathResources(locationPattern.substring(
CLASSPATH_ALL_URL_PREFIX.length()));}
</span>
}
else ...{
// Only look for a pattern after a prefix here
// (to not get fooled by a pattern symbol in a strange prefix).
int prefixEnd = locationPattern.indexOf(":") + 1;
if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) ...{
// a file pattern
return findPathMatchingResources(locationPattern);
}
else ...{
// a single resource with the given name
return new Resource[] ...{getResourceLoader().getResource(locationPattern)};
}
}
}
