From 162bba05d50f7a80a7676c3df1733d2b4e747f39 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 21 Mar 2014 18:17:52 +0100 Subject: [PATCH] DATAREST-280 - Improvements in RepositoryRestConfiguration. Removed custom subclass to use @EnableHypermediaSupport as this was effectively a workaround for SPR-11251, fixed in Spring 3.2.7. Move away from autowiring all MappingContexts into the configuration as this will require the instances to be created when the configuration class is prepared. We now rather inspect the BeanFactory on access of persistentEntities(). --- .../RepositoryRestMvcConfiguration.java | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java index 36b76186b..08590d935 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java @@ -22,6 +22,7 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -31,6 +32,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.Lazy; import org.springframework.context.support.MessageSourceAccessor; @@ -80,6 +82,7 @@ import org.springframework.data.util.AnnotatedTypeScanner; import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver; import org.springframework.data.web.HateoasSortHandlerMethodArgumentResolver; import org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration; +import org.springframework.data.web.config.SpringDataJacksonConfiguration; import org.springframework.format.support.DefaultFormattingConversionService; import org.springframework.hateoas.EntityLinks; import org.springframework.hateoas.MediaTypes; @@ -118,24 +121,13 @@ import com.fasterxml.jackson.databind.SerializationFeature; * @author Oliver Gierke */ @Configuration +@EnableHypermediaSupport(type = HypermediaType.HAL) @ComponentScan(basePackageClasses = RepositoryRestController.class, includeFilters = @Filter(RepositoryRestController.class), useDefaultFilters = false) @ImportResource("classpath*:META-INF/spring-data-rest/**/*.xml") +@Import(SpringDataJacksonConfiguration.class) public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebConfiguration { - /** - * Helper class instead of using {@link EnableHypermediaSupport} directly to make sure the annotation gets inspected - * correctly even if users extend {@link RepositoryRestMvcConfiguration}. - * - * @see https://jira.springsource.org/browse/SPR-11251 - * @author Oliver Gierke - */ - @Configuration - @EnableHypermediaSupport(type = HypermediaType.HAL) - static class HypermediaConfigurationDelegate { - - } - private static final boolean IS_JAVAX_VALIDATION_AVAILABLE = ClassUtils.isPresent( "javax.validation.ConstraintViolationException", RepositoryRestMvcConfiguration.class.getClassLoader()); private static final boolean IS_JPA_AVAILABLE = ClassUtils.isPresent("javax.persistence.EntityManager", @@ -143,7 +135,6 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon @Autowired ListableBeanFactory beanFactory; - @Autowired(required = false) List> mappingContexts = Collections.emptyList(); @Autowired(required = false) List> resourceProcessors = Collections.emptyList(); @Autowired(required = false) List idConverters = Collections.emptyList(); @@ -162,7 +153,15 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon @Bean public PersistentEntities persistentEntities() { - return new PersistentEntities(mappingContexts); + + List> arrayList = new ArrayList>(); + + for (MappingContext context : BeanFactoryUtils.beansOfTypeIncludingAncestors(beanFactory, + MappingContext.class).values()) { + arrayList.add(context); + } + + return new PersistentEntities(arrayList); } @Bean