Skip to content

Commit 16263e3

Browse files
committed
Move bean definition counting only used in tests into test code
Closes gh-22105
1 parent fe78be2 commit 16263e3

File tree

2 files changed

+53
-50
lines changed

2 files changed

+53
-50
lines changed

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

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -120,51 +120,43 @@ void setEnvironment(ConfigurableEnvironment environment) {
120120

121121
/**
122122
* Load the sources into the reader.
123-
* @return the number of loaded beans
124123
*/
125-
int load() {
126-
int count = 0;
124+
void load() {
127125
for (Object source : this.sources) {
128-
count += load(source);
126+
load(source);
129127
}
130-
return count;
131128
}
132129

133-
private int load(Object source) {
130+
private void load(Object source) {
134131
Assert.notNull(source, "Source must not be null");
135132
if (source instanceof Class<?>) {
136-
return load((Class<?>) source);
133+
load((Class<?>) source);
134+
return;
137135
}
138136
if (source instanceof Resource) {
139-
return load((Resource) source);
137+
load((Resource) source);
138+
return;
140139
}
141140
if (source instanceof Package) {
142-
return load((Package) source);
141+
load((Package) source);
142+
return;
143143
}
144144
if (source instanceof CharSequence) {
145-
return load((CharSequence) source);
145+
load((CharSequence) source);
146+
return;
146147
}
147148
throw new IllegalArgumentException("Invalid source type " + source.getClass());
148149
}
149150

150-
private int load(Class<?> source) {
151+
private void load(Class<?> source) {
151152
if (isGroovyPresent() && GroovyBeanDefinitionSource.class.isAssignableFrom(source)) {
152153
// Any GroovyLoaders added in beans{} DSL can contribute beans here
153154
GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source, GroovyBeanDefinitionSource.class);
154-
load(loader);
155+
((GroovyBeanDefinitionReader) this.groovyReader).beans(loader.getBeans());
155156
}
156157
if (isEligible(source)) {
157158
this.annotatedReader.register(source);
158-
return 1;
159159
}
160-
return 0;
161-
}
162-
163-
private int load(GroovyBeanDefinitionSource source) {
164-
int before = this.xmlReader.getRegistry().getBeanDefinitionCount();
165-
((GroovyBeanDefinitionReader) this.groovyReader).beans(source.getBeans());
166-
int after = this.xmlReader.getRegistry().getBeanDefinitionCount();
167-
return after - before;
168160
}
169161

170162
private int load(Resource source) {
@@ -181,36 +173,41 @@ private int load(Package source) {
181173
return this.scanner.scan(source.getName());
182174
}
183175

184-
private int load(CharSequence source) {
176+
private void load(CharSequence source) {
185177
String resolvedSource = this.xmlReader.getEnvironment().resolvePlaceholders(source.toString());
186178
// Attempt as a Class
187179
try {
188-
return load(ClassUtils.forName(resolvedSource, null));
180+
load(ClassUtils.forName(resolvedSource, null));
181+
return;
189182
}
190183
catch (IllegalArgumentException | ClassNotFoundException ex) {
191184
// swallow exception and continue
192185
}
193-
// Attempt as resources
194-
Resource[] resources = findResources(resolvedSource);
195-
int loadCount = 0;
196-
boolean atLeastOneResourceExists = false;
197-
for (Resource resource : resources) {
198-
if (isLoadCandidate(resource)) {
199-
atLeastOneResourceExists = true;
200-
loadCount += load(resource);
201-
}
202-
}
203-
if (atLeastOneResourceExists) {
204-
return loadCount;
186+
// Attempt as Resources
187+
if (loadAsResources(resolvedSource)) {
188+
return;
205189
}
206190
// Attempt as package
207191
Package packageResource = findPackage(resolvedSource);
208192
if (packageResource != null) {
209-
return load(packageResource);
193+
load(packageResource);
194+
return;
210195
}
211196
throw new IllegalArgumentException("Invalid source '" + resolvedSource + "'");
212197
}
213198

199+
private boolean loadAsResources(String resolvedSource) {
200+
boolean foundCandidate = false;
201+
Resource[] resources = findResources(resolvedSource);
202+
for (Resource resource : resources) {
203+
if (isLoadCandidate(resource)) {
204+
foundCandidate = true;
205+
load(resource);
206+
}
207+
}
208+
return foundCandidate;
209+
}
210+
214211
private boolean isGroovyPresent() {
215212
return ClassUtils.isPresent("groovy.lang.MetaClass", null);
216213
}

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

Lines changed: 20 additions & 14 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.
@@ -51,7 +51,7 @@ void cleanUp() {
5151
@Test
5252
void loadClass() {
5353
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class);
54-
assertThat(loader.load()).isEqualTo(1);
54+
assertThat(load(loader)).isEqualTo(1);
5555
assertThat(this.registry.containsBean("myComponent")).isTrue();
5656
}
5757

@@ -61,21 +61,21 @@ void anonymousClassNotLoaded() {
6161

6262
};
6363
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, myComponent.getClass());
64-
assertThat(loader.load()).isEqualTo(0);
64+
assertThat(load(loader)).isEqualTo(0);
6565
}
6666

6767
@Test
6868
void loadJsr330Class() {
6969
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyNamedComponent.class);
70-
assertThat(loader.load()).isEqualTo(1);
70+
assertThat(load(loader)).isEqualTo(1);
7171
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
7272
}
7373

7474
@Test
7575
void loadXmlResource() {
7676
ClassPathResource resource = new ClassPathResource("sample-beans.xml", getClass());
7777
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
78-
assertThat(loader.load()).isEqualTo(1);
78+
assertThat(load(loader)).isEqualTo(1);
7979
assertThat(this.registry.containsBean("myXmlComponent")).isTrue();
8080

8181
}
@@ -84,7 +84,7 @@ void loadXmlResource() {
8484
void loadGroovyResource() {
8585
ClassPathResource resource = new ClassPathResource("sample-beans.groovy", getClass());
8686
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
87-
assertThat(loader.load()).isEqualTo(1);
87+
assertThat(load(loader)).isEqualTo(1);
8888
assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
8989

9090
}
@@ -93,46 +93,46 @@ void loadGroovyResource() {
9393
void loadGroovyResourceWithNamespace() {
9494
ClassPathResource resource = new ClassPathResource("sample-namespace.groovy", getClass());
9595
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, resource);
96-
assertThat(loader.load()).isEqualTo(1);
96+
assertThat(load(loader)).isEqualTo(1);
9797
assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
9898

9999
}
100100

101101
@Test
102102
void loadPackage() {
103103
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage());
104-
assertThat(loader.load()).isEqualTo(2);
104+
assertThat(load(loader)).isEqualTo(2);
105105
assertThat(this.registry.containsBean("myComponent")).isTrue();
106106
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
107107
}
108108

109109
@Test
110110
void loadClassName() {
111111
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getName());
112-
assertThat(loader.load()).isEqualTo(1);
112+
assertThat(load(loader)).isEqualTo(1);
113113
assertThat(this.registry.containsBean("myComponent")).isTrue();
114114
}
115115

116116
@Test
117117
void loadResourceName() {
118118
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
119119
"classpath:org/springframework/boot/sample-beans.xml");
120-
assertThat(loader.load()).isEqualTo(1);
120+
assertThat(load(loader)).isEqualTo(1);
121121
assertThat(this.registry.containsBean("myXmlComponent")).isTrue();
122122
}
123123

124124
@Test
125125
void loadGroovyName() {
126126
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
127127
"classpath:org/springframework/boot/sample-beans.groovy");
128-
assertThat(loader.load()).isEqualTo(1);
128+
assertThat(load(loader)).isEqualTo(1);
129129
assertThat(this.registry.containsBean("myGroovyComponent")).isTrue();
130130
}
131131

132132
@Test
133133
void loadPackageName() {
134134
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage().getName());
135-
assertThat(loader.load()).isEqualTo(2);
135+
assertThat(load(loader)).isEqualTo(2);
136136
assertThat(this.registry.containsBean("myComponent")).isTrue();
137137
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
138138
}
@@ -142,7 +142,7 @@ void loadPackageNameWithoutDot() {
142142
// See gh-6126
143143
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry,
144144
MyComponentInPackageWithoutDot.class.getPackage().getName());
145-
int loaded = loader.load();
145+
int loaded = load(loader);
146146
assertThat(loaded).isEqualTo(1);
147147
assertThat(this.registry.containsBean("myComponentInPackageWithoutDot")).isTrue();
148148
}
@@ -151,9 +151,15 @@ void loadPackageNameWithoutDot() {
151151
void loadPackageAndClassDoesNotDoubleAdd() {
152152
BeanDefinitionLoader loader = new BeanDefinitionLoader(this.registry, MyComponent.class.getPackage(),
153153
MyComponent.class);
154-
assertThat(loader.load()).isEqualTo(2);
154+
assertThat(load(loader)).isEqualTo(2);
155155
assertThat(this.registry.containsBean("myComponent")).isTrue();
156156
assertThat(this.registry.containsBean("myNamedComponent")).isTrue();
157157
}
158158

159+
private int load(BeanDefinitionLoader loader) {
160+
int beans = this.registry.getBeanDefinitionCount();
161+
loader.load();
162+
return this.registry.getBeanDefinitionCount() - beans;
163+
}
164+
159165
}

0 commit comments

Comments
 (0)