DATACMNS-330, DATACMNS-331 - Improved web configuration support.

Updated backing implementation of @EnableSpringDataWebSupport to use WebMvcConfigurerAdapter instead of WebMvcConfigurationSupport as the latter is a very custom beast in terms of configuration. Let the Spring HATEOAS specific configuration class extend the default one to register additional components. The default one accesses the FormattingConversionService through a qualified autowiring. It issues a warning in case none has been configured and asks the user to enable Spring MVC.

Updated Sonargraph architecture description to allow logging from web config layer.
This commit is contained in:
Oliver Gierke
2013-05-21 17:10:13 +02:00
parent a9abb40fbd
commit 16c3c15204
5 changed files with 42 additions and 26 deletions

View File

@@ -20,8 +20,6 @@ import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportSelector;
@@ -84,15 +82,8 @@ public @interface EnableSpringDataWebSupport {
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
List<String> configs = new ArrayList<String>();
if (HATEOAS_PRESENT) {
configs.add(HateoasAwareSpringDataWebConfiguration.class.getName());
}
configs.add(SpringDataWebConfiguration.class.getName());
return configs.toArray(new String[configs.size()]);
return new String[] { HATEOAS_PRESENT ? HateoasAwareSpringDataWebConfiguration.class.getName()
: SpringDataWebConfiguration.class.getName() };
}
}
}

View File

@@ -17,13 +17,11 @@ package org.springframework.data.web.config;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.data.web.PagedResourcesAssemblerArgumentResolver;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* JavaConfig class to register {@link PagedResourcesAssembler} and {@link PagedResourcesAssemblerArgumentResolver}.
@@ -32,19 +30,16 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupp
* @author Oliver Gierke
*/
@Configuration
class HateoasAwareSpringDataWebConfiguration extends WebMvcConfigurationSupport {
@Autowired
SpringDataWebConfiguration config;
class HateoasAwareSpringDataWebConfiguration extends SpringDataWebConfiguration {
@Bean
public PagedResourcesAssembler<?> pagedResourcesAssembler() {
return new PagedResourcesAssembler<Object>(config.pageableResolver(), null);
return new PagedResourcesAssembler<Object>(pageableResolver(), null);
}
@Bean
public PagedResourcesAssemblerArgumentResolver pagedResourcesAssemblerArgumentResolver() {
return new PagedResourcesAssemblerArgumentResolver(config.pageableResolver(), null);
return new PagedResourcesAssemblerArgumentResolver(pageableResolver(), null);
}
/*
@@ -52,8 +47,8 @@ class HateoasAwareSpringDataWebConfiguration extends WebMvcConfigurationSupport
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#addArgumentResolvers(java.util.List)
*/
@Override
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
config.addArgumentResolvers(argumentResolvers);
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
super.addArgumentResolvers(argumentResolvers);
argumentResolvers.add(pagedResourcesAssemblerArgumentResolver());
}
}

View File

@@ -17,6 +17,10 @@ package org.springframework.data.web.config;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.support.DomainClassConverter;
@@ -24,7 +28,7 @@ import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* Configuration class to register {@link PageableHandlerMethodArgumentResolver},
@@ -34,7 +38,13 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupp
* @author Oliver Gierke
*/
@Configuration
class SpringDataWebConfiguration extends WebMvcConfigurationSupport {
class SpringDataWebConfiguration extends WebMvcConfigurerAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringDataWebConfiguration.class);
@Autowired(required = false)
@Qualifier("mvcConversionService")
FormattingConversionService conversionService;
@Bean
public PageableHandlerMethodArgumentResolver pageableResolver() {
@@ -48,7 +58,16 @@ class SpringDataWebConfiguration extends WebMvcConfigurationSupport {
@Bean
public DomainClassConverter<FormattingConversionService> mvcDomainClassConverter() {
return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
if (conversionService == null) {
LOGGER.warn("No default Spring MVC FormattingConversionService registered! Have you forgotten to "
+ "use @EnableWebMvc or register a configuration class extending WebMvcConfigurationSupport?");
return null;
}
return new DomainClassConverter<FormattingConversionService>(conversionService);
}
/*