From 7e04ac2967b0bb3562b727d94a684a3d04136142 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 27 Jun 2024 15:26:35 +0100 Subject: [PATCH] Polish "Make it easier to override RequestToViewNameTranslator bean" See gh-40874 --- .../web/servlet/WebMvcAutoConfiguration.java | 2 -- .../servlet/WebMvcAutoConfigurationTests.java | 23 ++++++++++--------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java index 29aa7e3fdc3..937d4b4f0ed 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.java @@ -470,7 +470,6 @@ public class WebMvcAutoConfiguration { return super.flashMapManager(); } - @Override @Bean @ConditionalOnMissingBean(name = DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME) @@ -478,7 +477,6 @@ public class WebMvcAutoConfiguration { return super.viewNameTranslator(); } - private Resource getIndexHtmlResource() { for (String location : this.resourceProperties.getStaticLocations()) { Resource indexHtml = getIndexHtmlResource(location); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java index cd6ce32565c..6fb28fe2c90 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java @@ -65,7 +65,6 @@ import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebSe import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter; import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -90,7 +89,6 @@ import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.ParameterContentNegotiationStrategy; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; -import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.FormContentFilter; @@ -139,6 +137,7 @@ import org.springframework.web.servlet.support.AbstractFlashMapManager; import org.springframework.web.servlet.support.SessionFlashMapManager; import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; +import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator; import org.springframework.web.util.UrlPathHelper; import static org.assertj.core.api.Assertions.assertThat; @@ -408,21 +407,23 @@ class WebMvcAutoConfigurationTests { } @Test - public void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() { + void customViewNameTranslatorWithMatchingNameReplacesDefaultViewNameTranslator() { this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) .run((context) -> { - assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); - assertThat(context.getBean("viewNameTranslator")).isInstanceOf(SessionFlashMapManager.class); + assertThat(context).hasSingleBean(RequestToViewNameTranslator.class); + assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); }); } @Test - void customViewNameTranslatorWithDifferentNameReplaceDefaultViewNameTranslator() { - this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) - .run((context) -> { - assertThat(context).hasSingleBean(RequestToViewNameTranslator.class); - assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); - }); + void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() { + this.contextRunner + .withBean("customViewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) + .run((context) -> { + assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); + assertThat(context.getBean("viewNameTranslator")) + .isInstanceOf(DefaultRequestToViewNameTranslator.class); + }); } @Test