Skip to content

Commit 7faa606

Browse files
nosansnicoll
authored andcommitted
Restore proxying of @bean methods in @TestConfiguration
See gh-18675
1 parent bd4dc1e commit 7faa606

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/TestConfiguration.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.annotation.Target;
2424

2525
import org.springframework.boot.SpringBootConfiguration;
26+
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
2728
import org.springframework.core.annotation.AliasFor;
2829

@@ -51,4 +52,29 @@
5152
@AliasFor(annotation = Configuration.class)
5253
String value() default "";
5354

55+
/**
56+
* Specify whether {@link Bean @Bean} methods should get proxied in order to enforce
57+
* bean lifecycle behavior, e.g. to return shared singleton bean instances even in
58+
* case of direct {@code @Bean} method calls in user code. This feature requires
59+
* method interception, implemented through a runtime-generated CGLIB subclass which
60+
* comes with limitations such as the configuration class and its methods not being
61+
* allowed to declare {@code final}.
62+
* <p>
63+
* The default is {@code true}, allowing for 'inter-bean references' within the
64+
* configuration class as well as for external calls to this configuration's
65+
* {@code @Bean} methods, e.g. from another configuration class. If this is not needed
66+
* since each of this particular configuration's {@code @Bean} methods is
67+
* self-contained and designed as a plain factory method for container use, switch
68+
* this flag to {@code false} in order to avoid CGLIB subclass processing.
69+
* <p>
70+
* Turning off bean method interception effectively processes {@code @Bean} methods
71+
* individually like when declared on non-{@code @Configuration} classes, a.k.a.
72+
* "@Bean Lite Mode" (see {@link Bean @Bean's javadoc}). It is therefore behaviorally
73+
* equivalent to removing the {@code @Configuration} stereotype.
74+
* @since 2.2.1
75+
* @return whether to proxy {@code @Bean} methods
76+
*/
77+
@AliasFor(annotation = Configuration.class)
78+
boolean proxyBeanMethods() default true;
79+
5480
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.context;
18+
19+
import org.assertj.core.api.Assertions;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
23+
24+
/**
25+
* Tests for {@link TestConfiguration}.
26+
*
27+
* @author Dmytro Nosan
28+
*/
29+
class TestConfigurationTests {
30+
31+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
32+
33+
@Test
34+
void shouldProxyBeanMethods() {
35+
this.contextRunner.withUserConfiguration(ProxyBeanMethodsConfiguration.class)
36+
.run((context) -> Assertions.assertThat(context).hasFailed());
37+
}
38+
39+
@Test
40+
void shouldNotProxyBeanMethods() {
41+
this.contextRunner.withUserConfiguration(ProxyBeanMethodsDisableConfiguration.class)
42+
.run((context) -> Assertions.assertThat(context).hasNotFailed());
43+
}
44+
45+
@TestConfiguration
46+
final static class ProxyBeanMethodsConfiguration {
47+
48+
}
49+
50+
@TestConfiguration(proxyBeanMethods = false)
51+
final static class ProxyBeanMethodsDisableConfiguration {
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)