|
20 | 20 | import java.util.HashSet;
|
21 | 21 | import java.util.Set;
|
22 | 22 |
|
23 |
| -import org.springframework.beans.factory.BeanDefinitionStoreException; |
24 | 23 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
25 | 24 | import org.springframework.beans.factory.support.BeanNameGenerator;
|
26 | 25 | import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
@@ -154,56 +153,53 @@ private int load(Package source) {
|
154 | 153 | }
|
155 | 154 |
|
156 | 155 | private int load(CharSequence source) {
|
157 |
| - String sourceString = this.xmlReader.getEnvironment().resolvePlaceholders( |
| 156 | + |
| 157 | + String resolvedSource = this.xmlReader.getEnvironment().resolvePlaceholders( |
158 | 158 | source.toString());
|
| 159 | + |
| 160 | + // Attempt as a Class |
159 | 161 | 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)); |
162 | 163 | }
|
163 | 164 | catch (ClassNotFoundException ex) {
|
164 | 165 | // swallow exception and continue
|
165 | 166 | }
|
166 | 167 |
|
167 |
| - ResourceLoader loader = this.resourceLoader != null ? this.resourceLoader |
168 |
| - : DEFAULT_RESOURCE_LOADER; |
169 |
| - |
| 168 | + // Attempt as resources |
| 169 | + Resource[] resources = loadResources(resolvedSource); |
170 | 170 | 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); |
193 | 176 | }
|
194 | 177 | }
|
195 |
| - if (loadCount > 0) { |
| 178 | + if (atLeastOneResourceExists) { |
196 | 179 | return loadCount;
|
197 | 180 | }
|
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); |
204 | 197 | }
|
| 198 | + return new Resource[] { loader.getResource(source) }; |
| 199 | + } |
| 200 | + catch (IOException ex) { |
| 201 | + throw new IllegalStateException("Error reading source '" + source + "'"); |
205 | 202 | }
|
206 |
| - throw new IllegalArgumentException("Invalid source '" + source + "'"); |
207 | 203 | }
|
208 | 204 |
|
209 | 205 | private Package findPackage(CharSequence source) {
|
|
0 commit comments