Merge pull request #18675 from nosan
* pr/18675: Polish "Restore proxying of @Bean methods in @TestConfiguration" Restore proxying of @Bean methods in @TestConfiguration Closes gh-18675
This commit is contained in:
@@ -6840,7 +6840,7 @@ The following example shows a `RestDocumentationResultHandler` being defined:
|
||||
|
||||
[source,java,indent=0]
|
||||
----
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class ResultHandlerConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.springframework.restdocs.templates.TemplateFormats;
|
||||
public class AdvancedConfigurationExample {
|
||||
|
||||
// tag::configuration[]
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
public static class CustomizationConfiguration implements RestDocsRestAssuredConfigurationCustomizer {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation
|
||||
public class AdvancedConfigurationExample {
|
||||
|
||||
// tag::configuration[]
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
public static class CustomizationConfiguration implements RestDocsWebTestClientConfigurationCustomizer {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -49,7 +49,7 @@ class SampleWebClientTests {
|
||||
assertThat(headers.getLocation()).hasHost("other.example.com");
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class Config {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.springframework.boot.test.context.TestConfiguration;
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
@EntityScan("some.other.package")
|
||||
public class ExampleTestConfig {
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ class AutoConfigureTestDatabaseWithNoDatabaseIntegrationTests {
|
||||
assertThat(this.context.getBeanNamesForType(DataSource.class)).isNotEmpty();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ class MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests {
|
||||
assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class CustomizationConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -82,7 +82,7 @@ class RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests
|
||||
assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class CustomizationConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -72,7 +72,7 @@ class WebTestClientRestDocsAutoConfigurationAdvancedConfigurationIntegrationTest
|
||||
assertThat(new File(defaultSnippetsDir, "response-fields.md")).isFile();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class CustomizationConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -44,7 +44,7 @@ class WebMvcTestServletFilterRegistrationDisabledIntegrationTests {
|
||||
this.mvc.perform(get("/one")).andExpect(header().string("x-test", (String) null));
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class DisabledRegistrationConfiguration {
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
@@ -39,7 +40,7 @@ import org.springframework.core.annotation.AliasFor;
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@Configuration
|
||||
@TestComponent
|
||||
public @interface TestConfiguration {
|
||||
|
||||
@@ -51,4 +52,29 @@ public @interface TestConfiguration {
|
||||
@AliasFor(annotation = Configuration.class)
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* Specify whether {@link Bean @Bean} methods should get proxied in order to enforce
|
||||
* bean lifecycle behavior, e.g. to return shared singleton bean instances even in
|
||||
* case of direct {@code @Bean} method calls in user code. This feature requires
|
||||
* method interception, implemented through a runtime-generated CGLIB subclass which
|
||||
* comes with limitations such as the configuration class and its methods not being
|
||||
* allowed to declare {@code final}.
|
||||
* <p>
|
||||
* The default is {@code true}, allowing for 'inter-bean references' within the
|
||||
* configuration class as well as for external calls to this configuration's
|
||||
* {@code @Bean} methods, e.g. from another configuration class. If this is not needed
|
||||
* since each of this particular configuration's {@code @Bean} methods is
|
||||
* self-contained and designed as a plain factory method for container use, switch
|
||||
* this flag to {@code false} in order to avoid CGLIB subclass processing.
|
||||
* <p>
|
||||
* Turning off bean method interception effectively processes {@code @Bean} methods
|
||||
* individually like when declared on non-{@code @Configuration} classes, a.k.a.
|
||||
* "@Bean Lite Mode" (see {@link Bean @Bean's javadoc}). It is therefore behaviorally
|
||||
* equivalent to removing the {@code @Configuration} stereotype.
|
||||
* @return whether to proxy {@code @Bean} methods
|
||||
* @since 2.2.1
|
||||
*/
|
||||
@AliasFor(annotation = Configuration.class)
|
||||
boolean proxyBeanMethods() default true;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2012-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link TestConfiguration @TestConfiguration}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class TestConfigurationTests {
|
||||
|
||||
@Test
|
||||
void proxyBeanMethodsIsEnabledByDefault() {
|
||||
AnnotationAttributes attributes = AnnotatedElementUtils
|
||||
.getMergedAnnotationAttributes(DefaultTestConfiguration.class, Configuration.class);
|
||||
assertThat(attributes.get("proxyBeanMethods")).isEqualTo(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void proxyBeanMethodsCanBeDisabled() {
|
||||
AnnotationAttributes attributes = AnnotatedElementUtils
|
||||
.getMergedAnnotationAttributes(NoBeanMethodProxyingTestConfiguration.class, Configuration.class);
|
||||
assertThat(attributes.get("proxyBeanMethods")).isEqualTo(false);
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
static class DefaultTestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class NoBeanMethodProxyingTestConfiguration {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -67,7 +67,7 @@ class SpringBootTestContextBootstrapperIntegrationTests {
|
||||
assertThat(this.defaultTestExecutionListenersPostProcessorCalled).isTrue();
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
@TestConfiguration(proxyBeanMethods = false)
|
||||
static class TestConfig {
|
||||
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user