DATACMNS-475 - Added configuration class to auto-register GeoModule.

Added a JavaConfig class to declare a Spring bean for the newly introduced GeoModule. 

@EnableSpringDataWebSupport automatically registers this config class in case Jackson is on the classpath. This comes in handy in combination with Spring Boot which auto-registers all global Module beans with all global ObjectMappers.
This commit is contained in:
Oliver Gierke
2014-03-21 14:28:19 +01:00
parent b882d33d7b
commit de1e9ac684
4 changed files with 98 additions and 32 deletions

View File

@@ -10,15 +10,15 @@
<element type="TypeFilter" name="Assignment">
<element type="IncludeTypePattern" name="**.config.**"/>
</element>
<dependency toName="Project|spring-data-commons::Layer|Core::Subsystem|Geo" type="AllowedDependency"/>
<dependency toName="Project|spring-data-commons::Layer|Repositories::Subsystem|Support" type="AllowedDependency"/>
<dependency toName="External|External::Subsystem|Spring" type="AllowedDependency"/>
</element>
<dependency toName="Project|spring-data-commons::Layer|Application" type="AllowedDependency"/>
<dependency toName="Project|spring-data-commons::Layer|Core" type="AllowedDependency"/>
<dependency toName="Project|spring-data-commons::Layer|Repositories" type="AllowedDependency"/>
<dependency toName="External|External::Subsystem|Java Beans" type="AllowedDependency"/>
<dependency toName="External|External::Subsystem|Logging" type="AllowedDependency"/>
<dependency toName="External|External::Subsystem|Reflection" type="AllowedDependency"/>
<dependency toName="External|External::Subsystem|Servlet API" type="AllowedDependency"/>
<dependency toName="External|External::Subsystem|Spring" type="AllowedDependency"/>
</element>
<element type="Layer" name="Repositories">
@@ -143,6 +143,12 @@
<element type="TypeFilter" name="Assignment">
<element type="IncludeTypePattern" name="**.annotation.**"/>
</element>
<stereotype name="Public"/>
</element>
<element type="Subsystem" name="Geo">
<element type="TypeFilter" name="Assignment">
<element type="IncludeTypePattern" name="**.geo.**"/>
</element>
</element>
<element type="Subsystem" name="Authentication">
<element type="TypeFilter" name="Assignment">
@@ -163,19 +169,11 @@
<element type="TypeFilter" name="Assignment">
<element type="IncludeTypePattern" name="**.transaction.**"/>
</element>
<stereotype name="Unrestricted"/>
</element>
<element type="Subsystem" name="Configuration">
<element type="TypeFilter" name="Assignment">
<element type="WeakTypePattern" name="**.config.**"/>
</element>
<stereotype name="Unrestricted"/>
</element>
<element type="Subsystem" name="Geo">
<element type="TypeFilter" name="Assignment">
<element type="IncludeTypePattern" name="**.geo.**"/>
</element>
<stereotype name="Unrestricted"/>
</element>
<dependency toName="Project|spring-data-commons::Layer|Application" type="AllowedDependency"/>
</element>
@@ -269,11 +267,6 @@
<element type="IncludeTypePattern" name="com.mysema.query.**"/>
</element>
</element>
<element type="Subsystem" name="Servlet API">
<element type="TypeFilter" name="Assignment">
<element type="IncludeTypePattern" name="javax.servlet.**"/>
</element>
</element>
<element type="Subsystem" name="Java Beans">
<element type="TypeFilter" name="Assignment">
<element type="IncludeTypePattern" name="java.beans.**"/>
@@ -322,7 +315,12 @@
</element>
<element type="Subsystem" name="Jackson">
<element type="TypeFilter" name="Assignment">
<element type="IncludeTypePattern" name="org.codehaus.jackson.**"/>
<element type="IncludeTypePattern" name="com.fasterxml.jackson.**"/>
</element>
</element>
<element type="Subsystem" name="JRE">
<element type="TypeFilter" name="Assignment">
<element type="WeakTypePattern" name="java.**"/>
</element>
</element>
</architecture>

View File

@@ -20,6 +20,8 @@ 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;
@@ -80,6 +82,7 @@ public @interface EnableSpringDataWebSupport {
// Don't make final to allow test cases faking this to false
private static boolean HATEOAS_PRESENT = ClassUtils.isPresent("org.springframework.hateoas.Link", null);
private static boolean JACKSON_PRESENT = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", null);
/*
* (non-Javadoc)
@@ -87,8 +90,17 @@ public @interface EnableSpringDataWebSupport {
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[] { HATEOAS_PRESENT ? HateoasAwareSpringDataWebConfiguration.class.getName()
: SpringDataWebConfiguration.class.getName() };
List<String> imports = new ArrayList<String>();
imports.add(HATEOAS_PRESENT ? HateoasAwareSpringDataWebConfiguration.class.getName()
: SpringDataWebConfiguration.class.getName());
if (JACKSON_PRESENT) {
imports.add(SpringDataJacksonConfiguration.class.getName());
}
return imports.toArray(new String[imports.size()]);
}
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.web.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.geo.GeoModule;
/**
* JavaConfig class to export Jackson specific configuration.
*
* @author Oliver Gierke
*/
@Configuration
public class SpringDataJacksonConfiguration {
@Bean
public GeoModule jacksonGeoModule() {
return new GeoModule();
}
}

View File

@@ -17,7 +17,6 @@ package org.springframework.data.web.config;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
@@ -26,11 +25,9 @@ import java.util.List;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.SpringVersion;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.PagedResourcesAssemblerArgumentResolver;
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
@@ -49,6 +46,9 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
*/
public class EnableSpringDataWebSupportIntegrationTests {
private static final String HATEOAS = "HATEOAS_PRESENT";
private static final String JACKSON = "JACKSON_PRESENT";
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
@@ -56,14 +56,10 @@ public class EnableSpringDataWebSupportIntegrationTests {
}
@Before
public void setUp() {
assumeThat(SpringVersion.getVersion(), startsWith("3.2"));
}
@After
public void tearDown() {
reEnableHateoas();
reEnable(HATEOAS);
reEnable(JACKSON);
}
@Test
@@ -91,7 +87,7 @@ public class EnableSpringDataWebSupportIntegrationTests {
@Test
public void doesNotRegisterHateoasSpecificComponentsIfHateoasNotPresent() throws Exception {
hideHateoas();
hide(HATEOAS);
ApplicationContext context = WebTestUtils.createApplicationContext(SampleConfig.class);
List<String> names = Arrays.asList(context.getBeanDefinitionNames());
@@ -100,6 +96,32 @@ public class EnableSpringDataWebSupportIntegrationTests {
assertThat(names, not(hasItems("pagedResourcesAssembler", "pagedResourcesAssemblerArgumentResolver")));
}
/**
* @see DATACMNS-475
*/
@Test
public void registersJacksonSpecificBeanDefinitions() throws Exception {
ApplicationContext context = WebTestUtils.createApplicationContext(SampleConfig.class);
List<String> names = Arrays.asList(context.getBeanDefinitionNames());
assertThat(names, hasItem("jacksonGeoModule"));
}
/**
* @see DATACMNS-475
*/
@Test
public void doesNotRegisterJacksonSpecificComponentsIfJacksonNotPresent() throws Exception {
hide(JACKSON);
ApplicationContext context = WebTestUtils.createApplicationContext(SampleConfig.class);
List<String> names = Arrays.asList(context.getBeanDefinitionNames());
assertThat(names, not(hasItem("jacksonGeoModule")));
}
@SuppressWarnings("unchecked")
private static void assertResolversRegistered(ApplicationContext context, Class<?>... resolverTypes) {
@@ -116,16 +138,16 @@ public class EnableSpringDataWebSupportIntegrationTests {
assertThat(resolvers, hasItems(resolverMatchers.toArray(new Matcher[resolverMatchers.size()])));
}
private static void hideHateoas() throws Exception {
private static void hide(String module) throws Exception {
Field field = ReflectionUtils.findField(SpringDataWebConfigurationImportSelector.class, "HATEOAS_PRESENT");
Field field = ReflectionUtils.findField(SpringDataWebConfigurationImportSelector.class, module);
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, null, false);
}
private static void reEnableHateoas() {
private static void reEnable(String module) {
Field field = ReflectionUtils.findField(SpringDataWebConfigurationImportSelector.class, "HATEOAS_PRESENT");
Field field = ReflectionUtils.findField(SpringDataWebConfigurationImportSelector.class, module);
ReflectionUtils.makeAccessible(field);
ReflectionUtils.setField(field, null, true);
}