DATACMNS-660 - Web configuration support now picks up configuration annotated with @SpringDataWebConfigurationMixing.

Introduced @SpringDataWebConfigurationMixing that can be used on configuration classes that are supposed to contribute components to the Application context when @EnableSpringDataWebSupport is used.

SpringDataWebConfigurationImportSelector scans the org.springframework.data package for classes annotated with that annotation and adds them to the classes to be considered config classes.
This commit is contained in:
Oliver Gierke
2015-03-17 16:23:18 +01:00
parent d7adfbd859
commit 537fc430a9
5 changed files with 123 additions and 5 deletions

View File

@@ -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()]);

View File

@@ -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

View File

@@ -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 {
}