Skip to content

Commit 16c3c15

Browse files
committed
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.
1 parent a9abb40 commit 16c3c15

File tree

5 files changed

+42
-26
lines changed

5 files changed

+42
-26
lines changed

Spring Data Commons.sonargraph

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
<element type="TypeFilter" name="Assignment">
1111
<element type="IncludeTypePattern" name="**.config.**"/>
1212
</element>
13+
<dependency toName="External|External::Subsystem|Logging" type="AllowedDependency"/>
1314
<dependency toName="Project|spring-data-commons::Layer|Repositories::Subsystem|Support" type="AllowedDependency"/>
15+
<dependency toName="External|External::Subsystem|Spring" type="AllowedDependency"/>
1416
</element>
15-
<dependency toName="External|External" type="AllowedDependency"/>
1617
<dependency toName="Project|spring-data-commons::Layer|Application" type="AllowedDependency"/>
18+
<dependency toName="Project|spring-data-commons::Layer|Repositories" type="AllowedDependency"/>
1719
<dependency toName="External|External::Subsystem|Java Beans" type="AllowedDependency"/>
20+
<dependency toName="External|External::Subsystem|Logging" type="AllowedDependency"/>
1821
<dependency toName="External|External::Subsystem|Reflection" type="AllowedDependency"/>
1922
<dependency toName="External|External::Subsystem|Servlet API" type="AllowedDependency"/>
2023
<dependency toName="External|External::Subsystem|Spring" type="AllowedDependency"/>
@@ -154,6 +157,12 @@
154157
</element>
155158
<stereotype name="Unrestricted"/>
156159
</element>
160+
<element type="Subsystem" name="Transactions">
161+
<element type="TypeFilter" name="Assignment">
162+
<element type="IncludeTypePattern" name="**.transaction.**"/>
163+
</element>
164+
<stereotype name="Unrestricted"/>
165+
</element>
157166
<dependency toName="Project|spring-data-commons::Layer|Application" type="AllowedDependency"/>
158167
</element>
159168
<element type="Layer" name="Application">
@@ -278,7 +287,7 @@
278287
</element>
279288
<element type="Subsystem" name="Logging">
280289
<element type="TypeFilter" name="Assignment">
281-
<element type="IncludeTypePattern" name="org.slf4j.**"/>
290+
<element type="IncludeTypePattern" name="org.slf4j.*"/>
282291
</element>
283292
</element>
284293
<element type="Subsystem" name="AOP">

src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.lang.annotation.Retention;
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
23-
import java.util.ArrayList;
24-
import java.util.List;
2523

2624
import org.springframework.context.annotation.Import;
2725
import org.springframework.context.annotation.ImportSelector;
@@ -84,15 +82,8 @@ class SpringDataWebConfigurationImportSelector implements ImportSelector {
8482
*/
8583
@Override
8684
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
87-
88-
List<String> configs = new ArrayList<String>();
89-
90-
if (HATEOAS_PRESENT) {
91-
configs.add(HateoasAwareSpringDataWebConfiguration.class.getName());
92-
}
93-
94-
configs.add(SpringDataWebConfiguration.class.getName());
95-
return configs.toArray(new String[configs.size()]);
85+
return new String[] { HATEOAS_PRESENT ? HateoasAwareSpringDataWebConfiguration.class.getName()
86+
: SpringDataWebConfiguration.class.getName() };
9687
}
9788
}
9889
}

src/main/java/org/springframework/data/web/config/HateoasAwareSpringDataWebConfiguration.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
import java.util.List;
1919

20-
import org.springframework.beans.factory.annotation.Autowired;
2120
import org.springframework.context.annotation.Bean;
2221
import org.springframework.context.annotation.Configuration;
2322
import org.springframework.data.web.PagedResourcesAssembler;
2423
import org.springframework.data.web.PagedResourcesAssemblerArgumentResolver;
2524
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
26-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
2725

2826
/**
2927
* JavaConfig class to register {@link PagedResourcesAssembler} and {@link PagedResourcesAssemblerArgumentResolver}.
@@ -32,28 +30,25 @@
3230
* @author Oliver Gierke
3331
*/
3432
@Configuration
35-
class HateoasAwareSpringDataWebConfiguration extends WebMvcConfigurationSupport {
36-
37-
@Autowired
38-
SpringDataWebConfiguration config;
33+
class HateoasAwareSpringDataWebConfiguration extends SpringDataWebConfiguration {
3934

4035
@Bean
4136
public PagedResourcesAssembler<?> pagedResourcesAssembler() {
42-
return new PagedResourcesAssembler<Object>(config.pageableResolver(), null);
37+
return new PagedResourcesAssembler<Object>(pageableResolver(), null);
4338
}
4439

4540
@Bean
4641
public PagedResourcesAssemblerArgumentResolver pagedResourcesAssemblerArgumentResolver() {
47-
return new PagedResourcesAssemblerArgumentResolver(config.pageableResolver(), null);
42+
return new PagedResourcesAssemblerArgumentResolver(pageableResolver(), null);
4843
}
4944

5045
/*
5146
* (non-Javadoc)
5247
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#addArgumentResolvers(java.util.List)
5348
*/
5449
@Override
55-
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
56-
config.addArgumentResolvers(argumentResolvers);
50+
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
51+
super.addArgumentResolvers(argumentResolvers);
5752
argumentResolvers.add(pagedResourcesAssemblerArgumentResolver());
5853
}
5954
}

src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717

1818
import java.util.List;
1919

20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.beans.factory.annotation.Qualifier;
2024
import org.springframework.context.annotation.Bean;
2125
import org.springframework.context.annotation.Configuration;
2226
import org.springframework.data.repository.support.DomainClassConverter;
2327
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
2428
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
2529
import org.springframework.format.support.FormattingConversionService;
2630
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
27-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
31+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
2832

2933
/**
3034
* Configuration class to register {@link PageableHandlerMethodArgumentResolver},
@@ -34,7 +38,13 @@
3438
* @author Oliver Gierke
3539
*/
3640
@Configuration
37-
class SpringDataWebConfiguration extends WebMvcConfigurationSupport {
41+
class SpringDataWebConfiguration extends WebMvcConfigurerAdapter {
42+
43+
private static final Logger LOGGER = LoggerFactory.getLogger(SpringDataWebConfiguration.class);
44+
45+
@Autowired(required = false)
46+
@Qualifier("mvcConversionService")
47+
FormattingConversionService conversionService;
3848

3949
@Bean
4050
public PageableHandlerMethodArgumentResolver pageableResolver() {
@@ -48,7 +58,16 @@ public SortHandlerMethodArgumentResolver sortResolver() {
4858

4959
@Bean
5060
public DomainClassConverter<FormattingConversionService> mvcDomainClassConverter() {
51-
return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
61+
62+
if (conversionService == null) {
63+
64+
LOGGER.warn("No default Spring MVC FormattingConversionService registered! Have you forgotten to "
65+
+ "use @EnableWebMvc or register a configuration class extending WebMvcConfigurationSupport?");
66+
67+
return null;
68+
}
69+
70+
return new DomainClassConverter<FormattingConversionService>(conversionService);
5271
}
5372

5473
/*

src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.data.web.config.EnableSpringDataWebSupport.SpringDataWebConfigurationImportSelector;
3636
import org.springframework.util.ReflectionUtils;
3737
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
38+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
3839
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
3940

4041
/**
@@ -46,6 +47,7 @@
4647
public class EnableSpringDataWebSupportIntegrationTests {
4748

4849
@Configuration
50+
@EnableWebMvc
4951
@EnableSpringDataWebSupport
5052
static class SampleConfig {
5153

0 commit comments

Comments
 (0)