Skip to content

Commit d8d8f9c

Browse files
encircledphilwebb
authored andcommitted
Allow beans without public constructors to load
Allow `BeanDefinitionLoader` to load classes that don't have public constructors. The constraint was first introduced in d82c508 to solve an issue with anonymous Groovy classes but causes particular problems with `@SpringBootTest`. See gh-20929
1 parent a0518d3 commit d8d8f9c

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,8 +31,6 @@
3131
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
3232
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
3333
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
34-
import org.springframework.core.annotation.MergedAnnotations;
35-
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
3634
import org.springframework.core.env.ConfigurableEnvironment;
3735
import org.springframework.core.io.ClassPathResource;
3836
import org.springframework.core.io.Resource;
@@ -41,7 +39,6 @@
4139
import org.springframework.core.io.support.ResourcePatternResolver;
4240
import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter;
4341
import org.springframework.core.type.filter.TypeFilter;
44-
import org.springframework.stereotype.Component;
4542
import org.springframework.util.Assert;
4643
import org.springframework.util.ClassUtils;
4744
import org.springframework.util.StringUtils;
@@ -53,6 +50,7 @@
5350
* {@link SpringApplication} for the types of sources that are supported.
5451
*
5552
* @author Phillip Webb
53+
* @author Vladislav Kisel
5654
* @see #setBeanNameGenerator(BeanNameGenerator)
5755
*/
5856
class BeanDefinitionLoader {
@@ -273,16 +271,14 @@ private Package findPackage(CharSequence source) {
273271
return Package.getPackage(source.toString());
274272
}
275273

274+
/**
275+
* Check whether the bean is eligible for registration.
276+
* @param type candidate bean type
277+
* @return true if the given bean type is eligible for registration, i.e. not a groovy
278+
* closure nor an anonymous class
279+
*/
276280
private boolean isComponent(Class<?> type) {
277-
// This has to be a bit of a guess. The only way to be sure that this type is
278-
// eligible is to make a bean definition out of it and try to instantiate it.
279-
if (MergedAnnotations.from(type, SearchStrategy.TYPE_HIERARCHY).isPresent(Component.class)) {
280-
return true;
281-
}
282-
// Nested anonymous classes are not eligible for registration, nor are groovy
283-
// closures
284-
return !type.getName().matches(".*\\$_.*closure.*") && !type.isAnonymousClass()
285-
&& type.getConstructors() != null && type.getConstructors().length != 0;
281+
return !type.getName().matches(".*\\$_.*closure.*") && !type.isAnonymousClass();
286282
}
287283

288284
/**

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/BeanDefinitionLoaderTests.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import sampleconfig.MyComponentInPackageWithoutDot;
2323

2424
import org.springframework.boot.sampleconfig.MyComponent;
25+
import org.springframework.boot.sampleconfig.MyNamedComponent;
2526
import org.springframework.context.support.StaticApplicationContext;
2627
import org.springframework.core.io.ClassPathResource;
2728

@@ -31,6 +32,7 @@
3132
* Tests for {@link BeanDefinitionLoader}.
3233
*
3334
* @author Phillip Webb
35+
* @author Vladislav Kisel
3436
*/
3537
class BeanDefinitionLoaderTests {
3638

@@ -53,6 +55,21 @@ void loadClass() {
5355
assertThat(this.registry.containsBean("myComponent")).isTrue();
5456
}
5557

58+
@Test
59+
void anonymousClassNotLoaded() {
60+
MyComponent myComponent = new MyComponent() {
61+
};
62+
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass());
63+
assertThat(loader.load()).isEqualTo(0);
64+
}
65+
66+
@Test
67+
void loadJsr330Class() {
68+
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyNamedComponent.class);
69+
assertThat(loader.load()).isEqualTo(1);
70+
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
71+
}
72+
5673
@Test
5774
void loadXmlResource() {
5875
ClassPathResource resource = new ClassPathResource("sample-beans.xml", getClass());
@@ -83,8 +100,9 @@ void loadGroovyResourceWithNamespace() {
83100
@Test
84101
void loadPackage() {
85102
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage());
86-
assertThat(loader.load()).isEqualTo(1);
103+
assertThat(loader.load()).isEqualTo(2);
87104
assertThat(this.registry.containsBean("myComponent")).isTrue();
105+
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
88106
}
89107

90108
@Test
@@ -113,8 +131,9 @@ void loadGroovyName() {
113131
@Test
114132
void loadPackageName() {
115133
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage().getName());
116-
assertThat(loader.load()).isEqualTo(1);
134+
assertThat(loader.load()).isEqualTo(2);
117135
assertThat(this.registry.containsBean("myComponent")).isTrue();
136+
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
118137
}
119138

120139
@Test
@@ -131,8 +150,9 @@ void loadPackageNameWithoutDot() {
131150
void loadPackageAndClassDoesNotDoubleAdd() {
132151
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage(),
133152
MyComponent.class);
134-
assertThat(loader.load()).isEqualTo(1);
153+
assertThat(loader.load()).isEqualTo(2);
135154
assertThat(this.registry.containsBean("myComponent")).isTrue();
155+
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
136156
}
137157

138158
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.sampleconfig;
18+
19+
import javax.inject.Named;
20+
21+
@Named
22+
public class MyNamedComponent {
23+
24+
MyNamedComponent() {
25+
26+
}
27+
28+
}

0 commit comments

Comments
 (0)