mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Guard for multiple ContentNegotiatingViewResolvers
Update WebMvcAutoConfiguration to ensure than the viewResolver bean is not created if a user defined ContentNegotiatingViewResolver bean is defined. Fixes gh-2269
This commit is contained in:
parent
f4e12e96c6
commit
c46478f97d
@ -188,7 +188,7 @@ public class WebMvcAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(ViewResolver.class)
|
||||
@ConditionalOnMissingBean(name = "viewResolver")
|
||||
@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
|
||||
public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
|
||||
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
|
||||
resolver.setContentNegotiationManager(beanFactory
|
||||
|
@ -51,6 +51,7 @@ import org.springframework.web.servlet.HandlerAdapter;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
@ -58,6 +59,7 @@ import org.springframework.web.servlet.i18n.FixedLocaleResolver;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
|
||||
import org.springframework.web.servlet.view.AbstractView;
|
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.instanceOf;
|
||||
@ -279,6 +281,31 @@ public class WebMvcAutoConfigurationTests {
|
||||
return mappingLocations;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customViewResolver() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(Config.class, CustomViewResolver.class,
|
||||
WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean("viewResolver"), instanceOf(MyViewResolver.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customContentNegotiatingViewResolver() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(Config.class, CustomContentNegotiatingViewResolver.class,
|
||||
WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
Map<String, ContentNegotiatingViewResolver> beans = this.context
|
||||
.getBeansOfType(ContentNegotiatingViewResolver.class);
|
||||
assertThat(beans.size(), equalTo(1));
|
||||
assertThat(beans.keySet().iterator().next(), equalTo("myViewResolver"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
protected static class ViewConfig {
|
||||
|
||||
@ -333,4 +360,33 @@ public class WebMvcAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class CustomViewResolver {
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
return new MyViewResolver();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class CustomContentNegotiatingViewResolver {
|
||||
|
||||
@Bean
|
||||
public ContentNegotiatingViewResolver myViewResolver() {
|
||||
return new ContentNegotiatingViewResolver();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class MyViewResolver implements ViewResolver {
|
||||
|
||||
@Override
|
||||
public View resolveViewName(String viewName, Locale locale) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user