Polish "Make it easier to override RequestToViewNameTranslator bean"

See gh-40874
This commit is contained in:
Andy Wilkinson 2024-06-27 15:26:35 +01:00
parent 062ed4ba2b
commit 7e04ac2967
2 changed files with 12 additions and 13 deletions

View File

@ -470,7 +470,6 @@ public class WebMvcAutoConfiguration {
return super.flashMapManager(); return super.flashMapManager();
} }
@Override @Override
@Bean @Bean
@ConditionalOnMissingBean(name = DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME) @ConditionalOnMissingBean(name = DispatcherServlet.REQUEST_TO_VIEW_NAME_TRANSLATOR_BEAN_NAME)
@ -478,7 +477,6 @@ public class WebMvcAutoConfiguration {
return super.viewNameTranslator(); return super.viewNameTranslator();
} }
private Resource getIndexHtmlResource() { private Resource getIndexHtmlResource() {
for (String location : this.resourceProperties.getStaticLocations()) { for (String location : this.resourceProperties.getStaticLocations()) {
Resource indexHtml = getIndexHtmlResource(location); Resource indexHtml = getIndexHtmlResource(location);

View File

@ -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.filter.OrderedFormContentFilter;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory; import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; 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.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; 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.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.FormContentFilter; 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.support.SessionFlashMapManager;
import org.springframework.web.servlet.view.AbstractView; import org.springframework.web.servlet.view.AbstractView;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator;
import org.springframework.web.util.UrlPathHelper; import org.springframework.web.util.UrlPathHelper;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -408,21 +407,23 @@ class WebMvcAutoConfigurationTests {
} }
@Test @Test
public void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() { void customViewNameTranslatorWithMatchingNameReplacesDefaultViewNameTranslator() {
this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new)
.run((context) -> { .run((context) -> {
assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); assertThat(context).hasSingleBean(RequestToViewNameTranslator.class);
assertThat(context.getBean("viewNameTranslator")).isInstanceOf(SessionFlashMapManager.class); assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class);
}); });
} }
@Test @Test
void customViewNameTranslatorWithDifferentNameReplaceDefaultViewNameTranslator() { void customViewNameTranslatorWithDifferentNameDoesNotReplaceDefaultViewNameTranslator() {
this.contextRunner.withBean("viewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new) this.contextRunner
.run((context) -> { .withBean("customViewNameTranslator", CustomViewNameTranslator.class, CustomViewNameTranslator::new)
assertThat(context).hasSingleBean(RequestToViewNameTranslator.class); .run((context) -> {
assertThat(context.getBean("viewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class); assertThat(context.getBean("customViewNameTranslator")).isInstanceOf(CustomViewNameTranslator.class);
}); assertThat(context.getBean("viewNameTranslator"))
.isInstanceOf(DefaultRequestToViewNameTranslator.class);
});
} }
@Test @Test