diff --git a/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java b/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java index 0a8e3e27e..f2f2a6963 100644 --- a/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java +++ b/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java @@ -23,9 +23,16 @@ import java.lang.annotation.Target; import java.util.ArrayList; import java.util.List; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.EnvironmentAware; +import org.springframework.context.ResourceLoaderAware; +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportSelector; +import org.springframework.core.env.Environment; +import org.springframework.core.io.ResourceLoader; import org.springframework.core.type.AnnotationMetadata; +import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; import org.springframework.util.ClassUtils; @@ -74,12 +81,33 @@ public @interface EnableSpringDataWebSupport { * * @author Oliver Gierke */ - class SpringDataWebConfigurationImportSelector implements ImportSelector { + class SpringDataWebConfigurationImportSelector implements ImportSelector, EnvironmentAware, ResourceLoaderAware { // 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); + private Environment environment; + private ResourceLoader resourceLoader; + + /* + * (non-Javadoc) + * @see org.springframework.context.EnvironmentAware#setEnvironment(org.springframework.core.env.Environment) + */ + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } + + /* + * (non-Javadoc) + * @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader) + */ + @Override + public void setResourceLoader(ResourceLoader resourceLoader) { + this.resourceLoader = resourceLoader; + } + /* * (non-Javadoc) * @see org.springframework.context.annotation.ImportSelector#selectImports(org.springframework.core.type.AnnotationMetadata) @@ -93,7 +121,15 @@ public @interface EnableSpringDataWebSupport { : SpringDataWebConfiguration.class.getName()); if (JACKSON_PRESENT) { - imports.add(SpringDataJacksonConfiguration.class.getName()); + + ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); + provider.setEnvironment(environment); + provider.setResourceLoader(resourceLoader); + provider.addIncludeFilter(new AnnotationTypeFilter(SpringDataWebConfigurationMixin.class)); + + for (BeanDefinition definition : provider.findCandidateComponents("org.springframework.data")) { + imports.add(definition.getBeanClassName()); + } } return imports.toArray(new String[imports.size()]); diff --git a/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java b/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java index eba007c31..3fd412e50 100644 --- a/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java +++ b/src/main/java/org/springframework/data/web/config/SpringDataJacksonConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 the original author or authors. + * Copyright 2014-2015 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. @@ -16,7 +16,6 @@ package org.springframework.data.web.config; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.data.geo.GeoModule; /** @@ -24,7 +23,7 @@ import org.springframework.data.geo.GeoModule; * * @author Oliver Gierke */ -@Configuration +@SpringDataWebConfigurationMixin public class SpringDataJacksonConfiguration { @Bean diff --git a/src/main/java/org/springframework/data/web/config/SpringDataWebConfigurationMixin.java b/src/main/java/org/springframework/data/web/config/SpringDataWebConfigurationMixin.java new file mode 100644 index 000000000..15b615245 --- /dev/null +++ b/src/main/java/org/springframework/data/web/config/SpringDataWebConfigurationMixin.java @@ -0,0 +1,41 @@ +/* + * Copyright 2015 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 java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.web.config.EnableSpringDataWebSupport.SpringDataWebConfigurationImportSelector; + +/** + * Annotation to be able to scan for additional Spring Data configuration classes to contribute to the web integration. + * + * @author Oliver Gierke + * @since 1.10 + * @soundtrack Selah Sue - This World (Selah Sue) + * @see SpringDataJacksonConfiguration + * @see SpringDataWebConfigurationImportSelector + */ +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Configuration +@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE }) +public @interface SpringDataWebConfigurationMixin { +} diff --git a/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java b/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java index 6fa6c1506..a1ee85905 100644 --- a/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java +++ b/src/test/java/org/springframework/data/web/config/EnableSpringDataWebSupportIntegrationTests.java @@ -172,6 +172,18 @@ public class EnableSpringDataWebSupportIntegrationTests { andExpect(status().isOk()); } + /** + * @see DATACMNS-660 + */ + @Test + public void picksUpWebConfigurationMixins() { + + ApplicationContext context = WebTestUtils.createApplicationContext(SampleConfig.class); + List names = Arrays.asList(context.getBeanDefinitionNames()); + + assertThat(names, hasItem("sampleBean")); + } + @SuppressWarnings("unchecked") private static void assertResolversRegistered(ApplicationContext context, Class... resolverTypes) { diff --git a/src/test/java/org/springframework/data/web/config/SampleMixin.java b/src/test/java/org/springframework/data/web/config/SampleMixin.java new file mode 100644 index 000000000..26e8001b3 --- /dev/null +++ b/src/test/java/org/springframework/data/web/config/SampleMixin.java @@ -0,0 +1,30 @@ +/* + * Copyright 2015 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; + +/** + * @author Oliver Gierke + */ +@SpringDataWebConfigurationMixin +public class SampleMixin { + + @Bean + String sampleBean() { + return "sampleBean"; + } +}