SPR-8350 ContentNegotiatingViewResolver initialization for nested ViewResolvers
This commit is contained in:
parent
100fed47f3
commit
5ac2e4418f
|
|
@ -146,6 +146,7 @@ class WebMvcConfiguration implements ApplicationContextAware, ServletContextAwar
|
|||
}
|
||||
|
||||
private MappedInterceptor[] getMappedInterceptors() {
|
||||
// TODO : prepare and store in instance var ?
|
||||
InterceptorConfigurer configurer = new InterceptorConfigurer();
|
||||
configurers.configureInterceptors(configurer);
|
||||
configurer.addInterceptor(new ConversionServiceExposingInterceptor(conversionService()));
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.servlet.view;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
|
@ -26,6 +27,7 @@ import java.util.Map;
|
|||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import javax.activation.FileTypeMap;
|
||||
import javax.activation.MimetypesFileTypeMap;
|
||||
import javax.servlet.ServletContext;
|
||||
|
|
@ -34,7 +36,6 @@ import javax.servlet.http.HttpServletResponse;
|
|||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.core.OrderComparator;
|
||||
import org.springframework.core.Ordered;
|
||||
|
|
@ -248,16 +249,28 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
|||
|
||||
@Override
|
||||
protected void initServletContext(ServletContext servletContext) {
|
||||
|
||||
Collection<ViewResolver> matchingBeans =
|
||||
BeanFactoryUtils.beansOfTypeIncludingAncestors(getApplicationContext(), ViewResolver.class).values();
|
||||
|
||||
if (this.viewResolvers == null) {
|
||||
Map<String, ViewResolver> matchingBeans =
|
||||
BeanFactoryUtils.beansOfTypeIncludingAncestors(getApplicationContext(), ViewResolver.class);
|
||||
this.viewResolvers = new ArrayList<ViewResolver>(matchingBeans.size());
|
||||
for (ViewResolver viewResolver : matchingBeans.values()) {
|
||||
for (ViewResolver viewResolver : matchingBeans) {
|
||||
if (this != viewResolver) {
|
||||
this.viewResolvers.add(viewResolver);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (int i=0; i < viewResolvers.size(); i++) {
|
||||
if (matchingBeans.contains(viewResolvers.get(i))) {
|
||||
continue;
|
||||
}
|
||||
String name = viewResolvers.get(i).getClass().getName() + i;
|
||||
getApplicationContext().getAutowireCapableBeanFactory().initializeBean(viewResolvers.get(i), name);
|
||||
}
|
||||
|
||||
}
|
||||
if (this.viewResolvers.isEmpty()) {
|
||||
logger.warn("Did not find any ViewResolvers to delegate to; please configure them using the " +
|
||||
"'viewResolvers' property on the ContentNegotiatingViewResolver");
|
||||
|
|
|
|||
|
|
@ -16,6 +16,15 @@
|
|||
|
||||
package org.springframework.web.servlet.view;
|
||||
|
||||
import static org.easymock.EasyMock.createMock;
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.EasyMock.replay;
|
||||
import static org.easymock.EasyMock.verify;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
|
@ -24,17 +33,16 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import org.junit.After;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
|
||||
|
|
@ -429,4 +437,24 @@ public class ContentNegotiatingViewResolverTests {
|
|||
verify(viewResolverMock, viewMock);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nestedViewResolverIsNotSpringBean() throws Exception {
|
||||
InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
|
||||
nestedResolver.setViewClass(InternalResourceView.class);
|
||||
viewResolver.setViewResolvers(new ArrayList<ViewResolver>(Arrays.asList(nestedResolver)));
|
||||
|
||||
StaticWebApplicationContext appContext = new StaticWebApplicationContext();
|
||||
appContext.setServletContext(new MockServletContext());
|
||||
appContext.refresh();
|
||||
viewResolver.setApplicationContext(appContext);
|
||||
|
||||
viewResolver.setDefaultContentType(MediaType.TEXT_HTML);
|
||||
|
||||
String viewName = "view";
|
||||
Locale locale = Locale.ENGLISH;
|
||||
|
||||
View result = viewResolver.resolveViewName(viewName, locale);
|
||||
assertNotNull("Invalid view", result);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue