Skip to content

Commit a4143b8

Browse files
author
Phillip Webb
committed
Polish pattern resolving in BeanDefinitionLoader
1 parent 507c4ec commit a4143b8

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

spring-boot/src/main/java/org/springframework/boot/BeanDefinitionLoader.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.HashSet;
2121
import java.util.Set;
2222

23-
import org.springframework.beans.factory.BeanDefinitionStoreException;
2423
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2524
import org.springframework.beans.factory.support.BeanNameGenerator;
2625
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@@ -154,56 +153,53 @@ private int load(Package source) {
154153
}
155154

156155
private int load(CharSequence source) {
157-
String sourceString = this.xmlReader.getEnvironment().resolvePlaceholders(
156+
157+
String resolvedSource = this.xmlReader.getEnvironment().resolvePlaceholders(
158158
source.toString());
159+
160+
// Attempt as a Class
159161
try {
160-
// Use class utils so that period separated nested class names work
161-
return load(ClassUtils.forName(sourceString, null));
162+
return load(ClassUtils.forName(resolvedSource, null));
162163
}
163164
catch (ClassNotFoundException ex) {
164165
// swallow exception and continue
165166
}
166167

167-
ResourceLoader loader = this.resourceLoader != null ? this.resourceLoader
168-
: DEFAULT_RESOURCE_LOADER;
169-
168+
// Attempt as resources
169+
Resource[] resources = loadResources(resolvedSource);
170170
int loadCount = 0;
171-
if (loader instanceof ResourcePatternResolver) {
172-
// Resource pattern matching available.
173-
try {
174-
Resource[] resources = ((ResourcePatternResolver) loader)
175-
.getResources(sourceString);
176-
for (Resource resource : resources) {
177-
if (resource.exists()) {
178-
loadCount += load(resource);
179-
}
180-
}
181-
}
182-
catch (IOException ex) {
183-
throw new BeanDefinitionStoreException(
184-
"Could not resolve bean definition resource pattern ["
185-
+ sourceString + "]", ex);
186-
}
187-
}
188-
if (!(loader instanceof ResourcePatternResolver)) {
189-
// Can only load single resources by absolute URL.
190-
Resource loadedResource = loader.getResource(sourceString);
191-
if (loadedResource != null && loadedResource.exists()) {
192-
return load(loadedResource);
171+
boolean atLeastOneResourceExists = false;
172+
for (Resource resource : resources) {
173+
if (resource != null && resource.exists()) {
174+
atLeastOneResourceExists = true;
175+
loadCount += load(resource);
193176
}
194177
}
195-
if (loadCount > 0) {
178+
if (atLeastOneResourceExists) {
196179
return loadCount;
197180
}
198-
else {
199-
// Attempt to treat the source as a package name, common to all
200-
// PatternResolver types
201-
Package packageResource = findPackage(source);
202-
if (packageResource != null) {
203-
return load(packageResource);
181+
182+
// Attempt as package
183+
Package packageResource = findPackage(resolvedSource);
184+
if (packageResource != null) {
185+
return load(packageResource);
186+
}
187+
188+
throw new IllegalArgumentException("Invalid source '" + resolvedSource + "'");
189+
}
190+
191+
private Resource[] loadResources(String source) {
192+
ResourceLoader loader = this.resourceLoader != null ? this.resourceLoader
193+
: DEFAULT_RESOURCE_LOADER;
194+
try {
195+
if (loader instanceof ResourcePatternResolver) {
196+
return ((ResourcePatternResolver) loader).getResources(source);
204197
}
198+
return new Resource[] { loader.getResource(source) };
199+
}
200+
catch (IOException ex) {
201+
throw new IllegalStateException("Error reading source '" + source + "'");
205202
}
206-
throw new IllegalArgumentException("Invalid source '" + source + "'");
207203
}
208204

209205
private Package findPackage(CharSequence source) {

0 commit comments

Comments
 (0)