Avoid registering a SpringDataWebSettings bean by default.
We now refrain from registering a SpringDataWebSettings instance as bean in the ApplicationContext if @EnableSpringDataWebSupport is used without an explicit declaration of pageSerializationMode. This allows Spring Boot to use the annotation, but allow the attribute value to be configured via a property at the same time. See https://github.com/spring-projects/spring-boot/pull/39797#discussion_r1508396169 for details. Fixes GH-3054.
This commit is contained in:
committed by
Oliver Drotbohm
parent
e994f6b60f
commit
e074fcc2dd
@@ -69,6 +69,7 @@ import org.springframework.util.ClassUtils;
|
||||
* @see SpringDataWebConfiguration
|
||||
* @see HateoasAwareSpringDataWebConfiguration
|
||||
* @author Oliver Gierke
|
||||
* @author Yanming Zhou
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
|
||||
@@ -170,6 +171,7 @@ public @interface EnableSpringDataWebSupport {
|
||||
* {@link EnableSpringDataWebSupport}.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
* @author Yanming Zhou
|
||||
* @soundtrack Norah Jones - Chasing Pirates
|
||||
* @since 3.3
|
||||
*/
|
||||
@@ -190,8 +192,14 @@ public @interface EnableSpringDataWebSupport {
|
||||
return;
|
||||
}
|
||||
|
||||
Object pageSerializationMode = attributes.get("pageSerializationMode");
|
||||
|
||||
if (pageSerializationMode == PageSerializationMode.DIRECT) {
|
||||
return;
|
||||
}
|
||||
|
||||
AbstractBeanDefinition definition = BeanDefinitionBuilder.rootBeanDefinition(SpringDataWebSettings.class)
|
||||
.addConstructorArgValue(attributes.get("pageSerializationMode"))
|
||||
.addConstructorArgValue(pageSerializationMode)
|
||||
.getBeanDefinition();
|
||||
|
||||
String beanName = importBeanNameGenerator.generateBeanName(definition, registry);
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -67,7 +68,7 @@ class EnableSpringDataWebSupportIntegrationTests {
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@EnableSpringDataWebSupport
|
||||
@EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.VIA_DTO)
|
||||
static class SampleConfig {
|
||||
|
||||
@Bean
|
||||
@@ -304,6 +305,10 @@ class EnableSpringDataWebSupportIntegrationTests {
|
||||
void usesDirectPageSerializationMode() throws Exception {
|
||||
|
||||
var applicationContext = WebTestUtils.createApplicationContext(PageSampleConfigWithDirect.class);
|
||||
|
||||
// SpringDataWebSettings shouldn't be registered if pageSerializationMode is default
|
||||
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> applicationContext.getBean(SpringDataWebSettings.class));
|
||||
|
||||
var mvc = MockMvcBuilders.webAppContextSetup(applicationContext).build();
|
||||
|
||||
mvc.perform(post("/page"))//
|
||||
|
||||
Reference in New Issue
Block a user