Polishing

This commit is contained in:
Juergen Hoeller
2014-09-17 19:10:33 +02:00
parent 3a1f7b6d14
commit e819999c08
15 changed files with 172 additions and 147 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 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.
@@ -25,7 +25,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Source;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@@ -134,12 +133,11 @@ import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolv
* libraries available on the classpath.
* </ul>
*
* @author Rossen Stoyanchev
* @since 3.1
* @see EnableWebMvc
* @see WebMvcConfigurer
* @see WebMvcConfigurerAdapter
*
* @author Rossen Stoyanchev
* @since 3.1
*/
public class WebMvcConfigurationSupport implements ApplicationContextAware, ServletContextAware {
@@ -158,10 +156,10 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", WebMvcConfigurationSupport.class.getClassLoader());
private ServletContext servletContext;
private ApplicationContext applicationContext;
private ServletContext servletContext;
private List<Object> interceptors;
private ContentNegotiationManager contentNegotiationManager;
@@ -169,6 +167,13 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
private List<HttpMessageConverter<?>> messageConverters;
/**
* Set the Spring {@link ApplicationContext}, e.g. for resource loading.
*/
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
/**
* Set the {@link javax.servlet.ServletContext}, e.g. for resource handling,
* looking up file extensions, etc.
@@ -177,13 +182,6 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
this.servletContext = servletContext;
}
/**
* Set the Spring {@link ApplicationContext}, e.g. for resource loading.
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
* requests to annotated controllers.
@@ -203,7 +201,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* use {@link #addInterceptors(InterceptorRegistry)} instead.
*/
protected final Object[] getInterceptors() {
if (interceptors == null) {
if (this.interceptors == null) {
InterceptorRegistry registry = new InterceptorRegistry();
addInterceptors(registry);
registry.addInterceptor(new ConversionServiceExposingInterceptor(mvcConversionService()));
@@ -233,8 +231,8 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
try {
this.contentNegotiationManager = configurer.getContentNegotiationManager();
}
catch (Exception e) {
throw new BeanInitializationException("Could not create ContentNegotiationManager", e);
catch (Exception ex) {
throw new BeanInitializationException("Could not create ContentNegotiationManager", ex);
}
}
return this.contentNegotiationManager;
@@ -273,7 +271,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
addViewControllers(registry);
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping();
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
handlerMapping.setInterceptors(getInterceptors());
return handlerMapping;
}
@@ -304,10 +302,11 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
*/
@Bean
public HandlerMapping resourceHandlerMapping() {
ResourceHandlerRegistry registry = new ResourceHandlerRegistry(applicationContext, servletContext);
ResourceHandlerRegistry registry = new ResourceHandlerRegistry(this.applicationContext, this.servletContext);
addResourceHandlers(registry);
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping();
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
return handlerMapping;
}
@@ -421,22 +420,16 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean";
clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
}
catch (ClassNotFoundException e) {
throw new BeanInitializationException("Could not find default validator", e);
catch (ClassNotFoundException ex) {
throw new BeanInitializationException("Could not find default validator class", ex);
}
catch (LinkageError e) {
throw new BeanInitializationException("Could not find default validator", e);
catch (LinkageError ex) {
throw new BeanInitializationException("Could not load default validator class", ex);
}
validator = (Validator) BeanUtils.instantiate(clazz);
}
else {
validator = new Validator() {
public boolean supports(Class<?> clazz) {
return false;
}
public void validate(Object target, Errors errors) {
}
};
validator = new NoOpValidator();
}
}
return validator;
@@ -494,14 +487,14 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
* used to add default message converters.
*/
protected final List<HttpMessageConverter<?>> getMessageConverters() {
if (messageConverters == null) {
messageConverters = new ArrayList<HttpMessageConverter<?>>();
configureMessageConverters(messageConverters);
if (messageConverters.isEmpty()) {
addDefaultHttpMessageConverters(messageConverters);
if (this.messageConverters == null) {
this.messageConverters = new ArrayList<HttpMessageConverter<?>>();
configureMessageConverters(this.messageConverters);
if (this.messageConverters.isEmpty()) {
addDefaultHttpMessageConverters(this.messageConverters);
}
}
return messageConverters;
return this.messageConverters;
}
/**
@@ -639,12 +632,26 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
exceptionResolvers.add(new DefaultHandlerExceptionResolver());
}
private final static class EmptyHandlerMapping extends AbstractHandlerMapping {
private static final class EmptyHandlerMapping extends AbstractHandlerMapping {
@Override
protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
protected Object getHandlerInternal(HttpServletRequest request) {
return null;
}
}
private static final class NoOpValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return false;
}
@Override
public void validate(Object target, Errors errors) {
}
}
}