Polish 'Allow beans without public constructors to load'

See gh-20929
This commit is contained in:
Phillip Webb 2020-06-06 11:59:36 -07:00
parent d8d8f9cf0b
commit c11abf48d9
3 changed files with 15 additions and 4 deletions

View File

@ -17,6 +17,7 @@
package org.springframework.boot;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.HashSet;
import java.util.Set;
@ -41,6 +42,7 @@ import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilte
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
@ -151,7 +153,7 @@ class BeanDefinitionLoader {
GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
load(loader);
}
if (isComponent(source)) {
if (isEligible(source)) {
this.annotatedReader.register(source);
return 1;
}
@ -277,8 +279,17 @@ class BeanDefinitionLoader {
* @return true if the given bean type is eligible for registration, i.e. not a groovy
* closure nor an anonymous class
*/
private boolean isComponent(Class<?> type) {
return !type.getName().matches(".*\\$_.*closure.*") && !type.isAnonymousClass();
private boolean isEligible(Class<?> type) {
return !(type.isAnonymousClass() || isGroovyClosure(type) || hasNoConstructors(type));
}
private boolean isGroovyClosure(Class<?> type) {
return type.getName().matches(".*\\$_.*closure.*");
}
private boolean hasNoConstructors(Class<?> type) {
Constructor<?>[] constructors = type.getDeclaredConstructors();
return ObjectUtils.isEmpty(constructors);
}
/**

View File

@ -58,6 +58,7 @@ class BeanDefinitionLoaderTests {
@Test
void anonymousClassNotLoaded() {
MyComponent myComponent = new MyComponent() {
};
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass());
assertThat(loader.load()).isEqualTo(0);

View File

@ -22,7 +22,6 @@ import javax.inject.Named;
public class MyNamedComponent {
MyNamedComponent() {
}
}