DATAKV-86 - Move configuration infrastructure for Map-based repositories.

We removed the EnableKeyValueRepositories in favor of store specific implementations. In this case EnableMapRepositories.

The configuration now defaults a KeyValueTemplate with a MapKeyValueAdapter but allows overriding the template in a bean named keyValueTemplate.
This commit is contained in:
Christoph Strobl
2014-11-28 15:10:35 +01:00
committed by Oliver Gierke
parent 4f37a7ecc3
commit ff1338a99c
8 changed files with 206 additions and 231 deletions

View File

@@ -1,205 +0,0 @@
/*
* 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.keyvalue.repository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.util.Assert;
/**
* @author Christoph Strobl
* @since 1.10
* @param <T>
* @param <ID>
*/
public class BasicKeyValueRepository<T, ID extends Serializable> implements KeyValueRepository<T, ID> {
private final KeyValueOperations operations;
private final EntityInformation<T, ID> entityInformation;
public BasicKeyValueRepository(EntityInformation<T, ID> metadata, KeyValueOperations operations) {
Assert.notNull(metadata, "Cannot initialize repository for 'null' metadata");
Assert.notNull(operations, "Cannot initialize repository for 'null' operations");
this.entityInformation = metadata;
this.operations = operations;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
*/
@Override
public Iterable<T> findAll(Sort sort) {
return operations.findAll(sort, entityInformation.getJavaType());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Pageable)
*/
@Override
public Page<T> findAll(Pageable pageable) {
List<T> content = null;
if (pageable.getSort() != null) {
content = operations.findInRange(pageable.getOffset(), pageable.getPageSize(), pageable.getSort(),
entityInformation.getJavaType());
} else {
content = operations.findInRange(pageable.getOffset(), pageable.getPageSize(), entityInformation.getJavaType());
}
return new PageImpl<T>(content, pageable, this.operations.count(entityInformation.getJavaType()));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
*/
@Override
public <S extends T> S save(S entity) {
Assert.notNull(entity, "Entity must not be 'null' for save.");
if (entityInformation.isNew(entity)) {
operations.insert(entity);
} else {
operations.update(entityInformation.getId(entity), entity);
}
return entity;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
*/
@Override
public <S extends T> Iterable<S> save(Iterable<S> entities) {
for (S entity : entities) {
save(entity);
}
return entities;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
*/
@Override
public T findOne(ID id) {
return operations.findById(id, entityInformation.getJavaType());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#exists(java.io.Serializable)
*/
@Override
public boolean exists(ID id) {
return findOne(id) != null;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll()
*/
@Override
<<<<<<< Updated upstream:src/main/java/org/springframework/data/keyvalue/repository/BasicKeyValueRepository.java
public Iterable<T> findAll() {
return operations.findAllOf(entityInformation.getJavaType());
=======
public List<T> findAll() {
return operations.findAll(entityInformation.getJavaType());
>>>>>>> Stashed changes:src/main/java/org/springframework/data/keyvalue/repository/support/SimpleKeyValueRepository.java
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
*/
@Override
public Iterable<T> findAll(Iterable<ID> ids) {
List<T> result = new ArrayList<T>();
for (ID id : ids) {
T candidate = findOne(id);
if (candidate != null) {
result.add(candidate);
}
}
return result;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#count()
*/
@Override
public long count() {
return operations.count(entityInformation.getJavaType());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.io.Serializable)
*/
@Override
public void delete(ID id) {
operations.delete(id, entityInformation.getJavaType());
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Object)
*/
@Override
public void delete(T entity) {
delete(entityInformation.getId(entity));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
*/
@Override
public void delete(Iterable<? extends T> entities) {
for (T entity : entities) {
delete(entity);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAll()
*/
@Override
public void deleteAll() {
operations.delete(entityInformation.getJavaType());
}
}

View File

@@ -32,16 +32,19 @@ import org.springframework.data.repository.config.AnnotationRepositoryConfigurat
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
import org.springframework.data.repository.config.RepositoryConfigurationSource;
import org.springframework.util.ObjectUtils;
/**
* {@link RepositoryConfigurationExtension} for {@link KeyValueRepository}.
*
* @author Christoph Strobl
* @since 1.10
* @author Oliver Gierke
*/
public class KeyValueRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
private static final String MAPPING_CONTEXT_BEAN_NAME = "keyValueMappingContext";
protected static final String MAPPING_CONTEXT_BEAN_NAME = "keyValueMappingContext";
protected static final String KEY_VALUE_TEMPLATE_BEAN_NAME = "keyValueTemplate";
protected static final String Key_VALUE_TEMPLATE_BEAN_NAME_CONFIG_ATTRIBUTE = KEY_VALUE_TEMPLATE_BEAN_NAME + "Ref";
/*
* (non-Javadoc)
@@ -88,7 +91,8 @@ public class KeyValueRepositoryConfigurationExtension extends RepositoryConfigur
AnnotationAttributes attributes = config.getAttributes();
builder.addPropertyReference("keyValueOperations", attributes.getString("keyValueTemplateRef"));
builder.addPropertyReference("keyValueOperations",
attributes.getString(Key_VALUE_TEMPLATE_BEAN_NAME_CONFIG_ATTRIBUTE));
builder.addPropertyValue("queryCreator", getQueryCreatorType(config));
builder.addPropertyReference("mappingContext", MAPPING_CONTEXT_BEAN_NAME);
}
@@ -123,11 +127,41 @@ public class KeyValueRepositoryConfigurationExtension extends RepositoryConfigur
super.registerBeansForRoot(registry, configurationSource);
if (!registry.containsBeanDefinition(MAPPING_CONTEXT_BEAN_NAME)) {
potentiallyRegisterMappingContext(registry, configurationSource);
potentiallyRegisterKeyValueTemplate(registry, configurationSource);
}
RootBeanDefinition mappingContextDefinition = new RootBeanDefinition(KeyValueMappingContext.class);
mappingContextDefinition.setSource(configurationSource.getSource());
registry.registerBeanDefinition(MAPPING_CONTEXT_BEAN_NAME, mappingContextDefinition);
private void potentiallyRegisterKeyValueTemplate(BeanDefinitionRegistry registry,
RepositoryConfigurationSource configurationSource) {
if (ObjectUtils.nullSafeEquals(configurationSource.getAttribute(Key_VALUE_TEMPLATE_BEAN_NAME_CONFIG_ATTRIBUTE),
KEY_VALUE_TEMPLATE_BEAN_NAME)) {
RootBeanDefinition defaultKeyValueTemplateBeanDefinition = getDefaultKeyValueTemplateBeanDefinition();
if (defaultKeyValueTemplateBeanDefinition != null) {
registerIfNotAlreadyRegistered(defaultKeyValueTemplateBeanDefinition, registry, KEY_VALUE_TEMPLATE_BEAN_NAME,
configurationSource);
}
}
}
private void potentiallyRegisterMappingContext(BeanDefinitionRegistry registry,
RepositoryConfigurationSource configurationSource) {
RootBeanDefinition mappingContextDefinition = new RootBeanDefinition(KeyValueMappingContext.class);
mappingContextDefinition.setSource(configurationSource.getSource());
registry.registerBeanDefinition(MAPPING_CONTEXT_BEAN_NAME, mappingContextDefinition);
registerIfNotAlreadyRegistered(mappingContextDefinition, registry, MAPPING_CONTEXT_BEAN_NAME, configurationSource);
}
/**
* Get the default {@link RootBeanDefinition} for {@link org.springframework.data.keyvalue.core.KeyValueTemplate}.
*
* @return {@literal null} to explicitly not register a template.
*/
protected RootBeanDefinition getDefaultKeyValueTemplateBeanDefinition() {
return null;
}
}

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.keyvalue.repository.config;
package org.springframework.data.map.repository.config;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
@@ -26,25 +26,26 @@ import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Import;
import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.repository.config.QueryCreatorType;
import org.springframework.data.keyvalue.repository.query.SpelQueryCreator;
import org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactoryBean;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
/**
* Annotation to activate KeyValue repositories. If no base package is configured through either {@link #value()},
* Annotation to activate Map repositories. If no base package is configured through either {@link #value()},
* {@link #basePackages()} or {@link #basePackageClasses()} it will trigger scanning of the package of annotated class.
*
* @author Christoph Strobl
* @since 1.10
* @author Oliver Gierke
*/
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(KeyValueRepositoriesRegistrar.class)
@Import(MapRepositoriesRegistrar.class)
@QueryCreatorType(SpelQueryCreator.class)
public @interface EnableKeyValueRepositories {
public @interface EnableMapRepositories {
/**
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.keyvalue.repository.config;
package org.springframework.data.map.repository.config;
import java.lang.annotation.Annotation;
@@ -21,12 +21,11 @@ import org.springframework.data.repository.config.RepositoryBeanDefinitionRegist
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
/**
* KeyValue specific {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar}
* Map specific {@link RepositoryBeanDefinitionRegistrarSupport} implementation.
*
* @author Christoph Strobl
* @since 1.10
*/
public class KeyValueRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
public class MapRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
/*
* (non-Javadoc)
@@ -34,7 +33,7 @@ public class KeyValueRepositoriesRegistrar extends RepositoryBeanDefinitionRegis
*/
@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableKeyValueRepositories.class;
return EnableMapRepositories.class;
}
/*
@@ -43,6 +42,6 @@ public class KeyValueRepositoriesRegistrar extends RepositoryBeanDefinitionRegis
*/
@Override
protected RepositoryConfigurationExtension getExtension() {
return new KeyValueRepositoryConfigurationExtension();
return new MapRepositoryConfigurationExtension();
}
}

View File

@@ -0,0 +1,62 @@
/*
* 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.map.repository.config;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.data.keyvalue.core.KeyValueTemplate;
import org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension;
import org.springframework.data.map.MapKeyValueAdapter;
/**
* @author Christoph Strobl
*/
public class MapRepositoryConfigurationExtension extends KeyValueRepositoryConfigurationExtension {
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension#getModuleName()
*/
@Override
public String getModuleName() {
return "Map";
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension#getModulePrefix()
*/
@Override
protected String getModulePrefix() {
return "map";
}
/*
* (non-Javadoc)
* @see org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension#getDefaultKeyValueTemplateBeanDefinition()
*/
@Override
protected RootBeanDefinition getDefaultKeyValueTemplateBeanDefinition() {
RootBeanDefinition keyValueTemplateDefinition = new RootBeanDefinition(KeyValueTemplate.class);
ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
constructorArgumentValues.addGenericArgumentValue(new RootBeanDefinition(MapKeyValueAdapter.class));
keyValueTemplateDefinition.setConstructorArgumentValues(constructorArgumentValues);
return keyValueTemplateDefinition;
}
}