Browse Source

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.
pull/3168/head
Yanming Zhou 2 years ago committed by Oliver Drotbohm
parent
commit
e70532770f
  1. 10
      src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java
  2. 7
      src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java

10
src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java

@ -69,6 +69,7 @@ import org.springframework.util.ClassUtils; @@ -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 { @@ -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 { @@ -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);

7
src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java

@ -24,6 +24,7 @@ import java.util.List; @@ -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 { @@ -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 { @@ -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"))//

Loading…
Cancel
Save