Skip to content

Commit 0c2e71c

Browse files
committed
Prevent early initialization of factory beans in text context customizers
Until Spring Framework 5.1.15, a FactoryBean with a non-default constructor defined via component scanning would cause an error. This behavior has changed as of spring-projects/spring-framework#22409. Regardless of this change we want to ensure that we avoid triggering eager initialisation. `SimpleFactoryBean` has been written this way so that the tests fail if early initialization is triggered regardless of the Spring Framework version. Fixes gh-15898
1 parent 274e9ed commit 0c2e71c

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-4
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplateContextCustomizer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public int getOrder() {
111111
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
112112
throws BeansException {
113113
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
114-
(ListableBeanFactory) this.beanFactory,
115-
TestRestTemplate.class).length == 0) {
114+
(ListableBeanFactory) this.beanFactory, TestRestTemplate.class, false,
115+
false).length == 0) {
116116
registry.registerBeanDefinition(TestRestTemplate.class.getName(),
117117
new RootBeanDefinition(TestRestTemplateFactory.class));
118118
}

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/reactive/server/WebTestClientContextCustomizer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public int getOrder() {
111111
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
112112
throws BeansException {
113113
if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
114-
(ListableBeanFactory) this.beanFactory,
115-
WebTestClient.class).length == 0) {
114+
(ListableBeanFactory) this.beanFactory, WebTestClient.class, false,
115+
false).length == 0) {
116116
registry.registerBeanDefinition(WebTestClient.class.getName(),
117117
new RootBeanDefinition(WebTestClientFactory.class));
118118
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2012-2019 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.test.web.client;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.context.SpringBootTest;
24+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
25+
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.ComponentScan;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.test.annotation.DirtiesContext;
30+
import org.springframework.test.context.junit4.SpringRunner;
31+
32+
/**
33+
* Integration tests for {@link TestRestTemplateContextCustomizer} to ensure
34+
* early-initialization of factory beans doesn't occur.
35+
*
36+
* @author Madhura Bhave
37+
*/
38+
@RunWith(SpringRunner.class)
39+
@SpringBootTest(classes = TestRestTemplateContextCustomizerWithFactoryBeanTests.TestClassWithFactoryBean.class, webEnvironment = WebEnvironment.RANDOM_PORT)
40+
@DirtiesContext
41+
public class TestRestTemplateContextCustomizerWithFactoryBeanTests {
42+
43+
@Autowired
44+
private TestRestTemplate restTemplate;
45+
46+
@Test
47+
public void test() {
48+
}
49+
50+
@Configuration
51+
@ComponentScan("org.springframework.boot.test.web.client.scan")
52+
static class TestClassWithFactoryBean {
53+
54+
@Bean
55+
public TomcatServletWebServerFactory webServerFactory() {
56+
return new TomcatServletWebServerFactory(0);
57+
}
58+
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2019 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+
package org.springframework.boot.test.web.client.scan;
17+
18+
import org.springframework.beans.factory.FactoryBean;
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.context.ApplicationContext;
21+
import org.springframework.stereotype.Component;
22+
23+
/**
24+
* @author Madhura Bhave
25+
*/
26+
@Component
27+
public class SimpleFactoryBean implements FactoryBean {
28+
29+
private static boolean isInitializedEarly = false;
30+
31+
public SimpleFactoryBean() {
32+
isInitializedEarly = true;
33+
throw new RuntimeException();
34+
}
35+
36+
@Autowired
37+
public SimpleFactoryBean(ApplicationContext context) {
38+
if (isInitializedEarly) {
39+
throw new RuntimeException();
40+
}
41+
}
42+
43+
public Object getObject() {
44+
return new Object();
45+
}
46+
47+
public Class<?> getObjectType() {
48+
return Object.class;
49+
}
50+
51+
}

0 commit comments

Comments
 (0)