DATAREST-473 - Introduced configuration option to control default exposure of repositories.

RepositoryRestConfiguration now exposes a setRepositoryDetectionStrategy(…) to define which repositories should be detected for exposure by default. The default value for that will consider the repository interfaces visibility but also take the exported flag of @(Repository)RestResource into account. See all other options in RepositoryDetectionStrategies.

Tweaked the auto-registration of excerpt projections to avoid a circular dependency between configuration and resource mappings and moved it onto a BeanPostProcessor implementation.
This commit is contained in:
Oliver Gierke
2015-11-25 11:32:34 +01:00
parent b663b5ff73
commit dc36dfb67a
13 changed files with 416 additions and 85 deletions

View File

@@ -0,0 +1,78 @@
/*
* 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.rest.webmvc.config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.core.mapping.RepositoryResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.util.Assert;
/**
* {@link BeanPostProcessor} to make sure all excerpt projections defined in {@link RepositoryResourceMappings} are
* registered with the {@link RepositoryRestConfiguration}. This rather external configuration has been used to make
* sure we don't introduce a cyclic dependency between {@link RepositoryRestConfiguration} an
* {@link RepositoryResourceMappings} as the latter need access to the former to discover mappings in the first place.
*
* @author Oliver Gierke
* @since 2.5
* @soundtrack Katinka - Ausverkauf
*/
public class ProjectionDefinitionRegistar extends InstantiationAwareBeanPostProcessorAdapter {
private final ObjectFactory<RepositoryRestConfiguration> config;
/**
* Creates a new {@link ProjectionDefinitionRegistar} for the given {@link RepositoryRestConfiguration}.
*
* @param config must not be {@literal null}.
*/
public ProjectionDefinitionRegistar(ObjectFactory<RepositoryRestConfiguration> config) {
Assert.notNull(config, "RepositoryRestConfiguration must not be null!");
this.config = config;
}
/*
* (non-Javadoc)
* @see org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter#postProcessAfterInitialization(java.lang.Object, java.lang.String)
*/
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (!(bean instanceof ResourceMappings)) {
return bean;
}
ResourceMappings mappings = (ResourceMappings) bean;
for (ResourceMetadata resourceMetadata : mappings) {
Class<?> projection = resourceMetadata.getExcerptProjection();
if (projection != null) {
config.getObject().getProjectionConfiguration().addProjection(projection);
}
}
return bean;
}
}

View File

@@ -244,7 +244,7 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
@Bean
public RepositoryRestConfiguration config() {
ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration(resourceMappings());
ProjectionDefinitionConfiguration configuration = new ProjectionDefinitionConfiguration();
// Register projections found in packages
for (Class<?> projection : getProjections(repositories())) {
@@ -259,6 +259,11 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
return config;
}
@Bean
public ProjectionDefinitionRegistar projectionDefinitionRegistrar(ObjectFactory<RepositoryRestConfiguration> config) {
return new ProjectionDefinitionRegistar(config);
}
@Bean
public MetadataConfiguration metadataConfiguration() {
return new MetadataConfiguration();
@@ -568,8 +573,9 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
}
@Bean
public ResourceMappings resourceMappings() {
return new RepositoryResourceMappings(repositories(), persistentEntities());
public RepositoryResourceMappings resourceMappings() {
return new RepositoryResourceMappings(repositories(), persistentEntities(),
config().getRepositoryDetectionStrategy());
}
/**

View File

@@ -108,7 +108,8 @@ public class RepositoryTestsConfig {
@Bean
public Module persistentEntityModule() {
RepositoryResourceMappings mappings = new RepositoryResourceMappings(repositories(), persistentEntities());
RepositoryResourceMappings mappings = new RepositoryResourceMappings(repositories(), persistentEntities(),
config().getRepositoryDetectionStrategy());
EntityLinks entityLinks = new RepositoryEntityLinks(repositories(), mappings, config(),
mock(PagingAndSortingTemplateVariables.class),
OrderAwarePluginRegistry.<Class<?>, BackendIdConverter> create(Arrays.asList(DefaultIdConverter.INSTANCE)));