Commit c11abf48 authored by Phillip Webb's avatar Phillip Webb

Polish 'Allow beans without public constructors to load'

See gh-20929
parent d8d8f9cf
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
package org.springframework.boot; package org.springframework.boot;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
...@@ -41,6 +42,7 @@ import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilte ...@@ -41,6 +42,7 @@ import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilte
import org.springframework.core.type.filter.TypeFilter; import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
...@@ -151,7 +153,7 @@ class BeanDefinitionLoader { ...@@ -151,7 +153,7 @@ class BeanDefinitionLoader {
GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class); GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
load(loader); load(loader);
} }
if (isComponent(source)) { if (isEligible(source)) {
this.annotatedReader.register(source); this.annotatedReader.register(source);
return 1; return 1;
} }
...@@ -277,8 +279,17 @@ class BeanDefinitionLoader { ...@@ -277,8 +279,17 @@ class BeanDefinitionLoader {
* @return true if the given bean type is eligible for registration, i.e. not a groovy * @return true if the given bean type is eligible for registration, i.e. not a groovy
* closure nor an anonymous class * closure nor an anonymous class
*/ */
private boolean isComponent(Class<?> type) { private boolean isEligible(Class<?> type) {
return !type.getName().matches(".*\\$_.*closure.*") && !type.isAnonymousClass(); 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);
} }
/** /**
......
...@@ -58,6 +58,7 @@ class BeanDefinitionLoaderTests { ...@@ -58,6 +58,7 @@ class BeanDefinitionLoaderTests {
@Test @Test
void anonymousClassNotLoaded() { void anonymousClassNotLoaded() {
MyComponent myComponent = new MyComponent() { MyComponent myComponent = new MyComponent() {
}; };
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass()); BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass());
assertThat(loader.load()).isEqualTo(0); assertThat(loader.load()).isEqualTo(0);
......
...@@ -22,7 +22,6 @@ import javax.inject.Named; ...@@ -22,7 +22,6 @@ import javax.inject.Named;
public class MyNamedComponent { public class MyNamedComponent {
MyNamedComponent() { MyNamedComponent() {
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment