Detect custom ContentNegotiationViewResolver

The <mvc:annotation-driven> element now adds an alias when a
ContentNegotiationManager bean is registered with a custom name.
This helps <mvc:view-resolvers> to more reliably find such a custom
ContentNegotiationManager.

Issue: SPR-13559
This commit is contained in:
Rossen Stoyanchev
2015-10-29 18:01:00 -04:00
parent 3e807c297d
commit f84a0c914a
5 changed files with 31 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -364,7 +364,11 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
private RuntimeBeanReference getContentNegotiationManager(Element element, Object source, ParserContext parserContext) {
RuntimeBeanReference contentNegotiationManagerRef;
if (element.hasAttribute("content-negotiation-manager")) {
contentNegotiationManagerRef = new RuntimeBeanReference(element.getAttribute("content-negotiation-manager"));
String name = element.getAttribute("content-negotiation-manager");
contentNegotiationManagerRef = new RuntimeBeanReference(name);
if (!CONTENT_NEGOTIATION_MANAGER_BEAN_NAME.equals(name)) {
parserContext.getRegistry().registerAlias(name, CONTENT_NEGOTIATION_MANAGER_BEAN_NAME);
}
}
else {
RootBeanDefinition factoryBeanDef = new RootBeanDefinition(ContentNegotiationManagerFactoryBean.class);
@@ -372,10 +376,10 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
factoryBeanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
factoryBeanDef.getPropertyValues().add("mediaTypes", getDefaultMediaTypes());
String beanName = CONTENT_NEGOTIATION_MANAGER_BEAN_NAME;
parserContext.getReaderContext().getRegistry().registerBeanDefinition(beanName , factoryBeanDef);
parserContext.registerComponent(new BeanComponentDefinition(factoryBeanDef, beanName));
contentNegotiationManagerRef = new RuntimeBeanReference(beanName);
String name = CONTENT_NEGOTIATION_MANAGER_BEAN_NAME;
parserContext.getReaderContext().getRegistry().registerBeanDefinition(name , factoryBeanDef);
parserContext.registerComponent(new BeanComponentDefinition(factoryBeanDef, name));
contentNegotiationManagerRef = new RuntimeBeanReference(name);
}
return contentNegotiationManagerRef;
}

View File

@@ -192,9 +192,9 @@ public class ViewResolversBeanDefinitionParser implements BeanDefinitionParser {
if (resolverElement.hasAttribute("use-not-acceptable")) {
values.add("useNotAcceptableStatusCode", resolverElement.getAttribute("use-not-acceptable"));
}
String beanName = AnnotationDrivenBeanDefinitionParser.CONTENT_NEGOTIATION_MANAGER_BEAN_NAME;
if (context.getRegistry().containsBeanDefinition(beanName)) {
values.add("contentNegotiationManager", new RuntimeBeanReference(beanName));
String name = AnnotationDrivenBeanDefinitionParser.CONTENT_NEGOTIATION_MANAGER_BEAN_NAME;
if (context.getRegistry().containsBeanDefinition(name) || context.getRegistry().isAlias(name)) {
values.add("contentNegotiationManager", new RuntimeBeanReference(name));
}
return beanDef;
}

View File

@@ -119,6 +119,10 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
this.contentNegotiationManager = contentNegotiationManager;
}
public ContentNegotiationManager getContentNegotiationManager() {
return this.contentNegotiationManager;
}
/**
* Indicate whether a {@link HttpServletResponse#SC_NOT_ACCEPTABLE 406 Not Acceptable}
* status code should be returned if no suitable view can be found.