Polish pattern resolving in BeanDefinitionLoader
This commit is contained in:
parent
be79da139e
commit
5e6260ec5a
|
|
@ -20,7 +20,6 @@ import java.io.IOException;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||||
|
|
@ -154,56 +153,53 @@ class BeanDefinitionLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int load(CharSequence source) {
|
private int load(CharSequence source) {
|
||||||
String sourceString = this.xmlReader.getEnvironment().resolvePlaceholders(
|
|
||||||
|
String resolvedSource = this.xmlReader.getEnvironment().resolvePlaceholders(
|
||||||
source.toString());
|
source.toString());
|
||||||
|
|
||||||
|
// Attempt as a Class
|
||||||
try {
|
try {
|
||||||
// Use class utils so that period separated nested class names work
|
return load(ClassUtils.forName(resolvedSource, null));
|
||||||
return load(ClassUtils.forName(sourceString, null));
|
|
||||||
}
|
}
|
||||||
catch (ClassNotFoundException ex) {
|
catch (ClassNotFoundException ex) {
|
||||||
// swallow exception and continue
|
// swallow exception and continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ResourceLoader loader = this.resourceLoader != null ? this.resourceLoader
|
// Attempt as resources
|
||||||
: DEFAULT_RESOURCE_LOADER;
|
Resource[] resources = loadResources(resolvedSource);
|
||||||
|
|
||||||
int loadCount = 0;
|
int loadCount = 0;
|
||||||
if (loader instanceof ResourcePatternResolver) {
|
boolean atLeastOneResourceExists = false;
|
||||||
// Resource pattern matching available.
|
|
||||||
try {
|
|
||||||
Resource[] resources = ((ResourcePatternResolver) loader)
|
|
||||||
.getResources(sourceString);
|
|
||||||
for (Resource resource : resources) {
|
for (Resource resource : resources) {
|
||||||
if (resource.exists()) {
|
if (resource != null && resource.exists()) {
|
||||||
|
atLeastOneResourceExists = true;
|
||||||
loadCount += load(resource);
|
loadCount += load(resource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if (atLeastOneResourceExists) {
|
||||||
catch (IOException ex) {
|
|
||||||
throw new BeanDefinitionStoreException(
|
|
||||||
"Could not resolve bean definition resource pattern ["
|
|
||||||
+ sourceString + "]", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!(loader instanceof ResourcePatternResolver)) {
|
|
||||||
// Can only load single resources by absolute URL.
|
|
||||||
Resource loadedResource = loader.getResource(sourceString);
|
|
||||||
if (loadedResource != null && loadedResource.exists()) {
|
|
||||||
return load(loadedResource);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (loadCount > 0) {
|
|
||||||
return loadCount;
|
return loadCount;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// Attempt to treat the source as a package name, common to all
|
// Attempt as package
|
||||||
// PatternResolver types
|
Package packageResource = findPackage(resolvedSource);
|
||||||
Package packageResource = findPackage(source);
|
|
||||||
if (packageResource != null) {
|
if (packageResource != null) {
|
||||||
return load(packageResource);
|
return load(packageResource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Invalid source '" + resolvedSource + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
private Resource[] loadResources(String source) {
|
||||||
|
ResourceLoader loader = this.resourceLoader != null ? this.resourceLoader
|
||||||
|
: DEFAULT_RESOURCE_LOADER;
|
||||||
|
try {
|
||||||
|
if (loader instanceof ResourcePatternResolver) {
|
||||||
|
return ((ResourcePatternResolver) loader).getResources(source);
|
||||||
|
}
|
||||||
|
return new Resource[] { loader.getResource(source) };
|
||||||
|
}
|
||||||
|
catch (IOException ex) {
|
||||||
|
throw new IllegalStateException("Error reading source '" + source + "'");
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("Invalid source '" + source + "'");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Package findPackage(CharSequence source) {
|
private Package findPackage(CharSequence source) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue