mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Allow loading from package names without dots
Update BeanDefinitionLoader to support loading from package names that do not contain dots. Prior to this commit `new BeanDefinitionLoader(registry, "somepackage")` would fail because "somepackage" exists and is a resource but does not contain valid XML. Somewhat surprisingly the InputStream returned by the resource actually contains the listing of files in the package. Fixes gh-6126
This commit is contained in:
parent
1528764194
commit
a9b98cada5
@ -33,6 +33,7 @@ import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
@ -201,7 +202,7 @@ class BeanDefinitionLoader {
|
||||
int loadCount = 0;
|
||||
boolean atLeastOneResourceExists = false;
|
||||
for (Resource resource : resources) {
|
||||
if (resource != null && resource.exists()) {
|
||||
if (isLoadCandidate(resource)) {
|
||||
atLeastOneResourceExists = true;
|
||||
loadCount += load(resource);
|
||||
}
|
||||
@ -235,6 +236,28 @@ class BeanDefinitionLoader {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isLoadCandidate(Resource resource) {
|
||||
if (resource == null || !resource.exists()) {
|
||||
return false;
|
||||
}
|
||||
if (resource instanceof ClassPathResource) {
|
||||
// A simple package without a '.' may accidentally get loaded as an XML
|
||||
// document if we're not careful. The result of getInputStream() will be
|
||||
// a file list of the package content. We double check here that it's not
|
||||
// actually a package.
|
||||
String path = ((ClassPathResource) resource).getPath();
|
||||
if (path.indexOf(".") == -1) {
|
||||
try {
|
||||
return Package.getPackage(path) == null;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private Package findPackage(CharSequence source) {
|
||||
Package pkg = Package.getPackage(source.toString());
|
||||
if (pkg != null) {
|
||||
|
@ -19,6 +19,7 @@ package org.springframework.boot;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import sampleconfig.MyComponentInPackageWithoutDot;
|
||||
|
||||
import org.springframework.boot.sampleconfig.MyComponent;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
@ -134,6 +135,16 @@ public class BeanDefinitionLoaderTests {
|
||||
assertTrue(this.registry.containsBean("myComponent"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadPackageNameWithoutDot() throws Exception {
|
||||
// See gh-6126
|
||||
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
|
||||
MyComponentInPackageWithoutDot.class.getPackage().getName());
|
||||
int loaded = loader.load();
|
||||
assertThat(loaded, equalTo(1));
|
||||
assertTrue(this.registry.containsBean("myComponentInPackageWithoutDot"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadPackageAndClassDoesNotDoubleAdd() throws Exception {
|
||||
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
|
||||
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sampleconfig;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MyComponentInPackageWithoutDot {
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user