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

@@ -1,123 +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.config;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
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.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()},
* {@link #basePackages()} or {@link #basePackageClasses()} it will trigger scanning of the package of annotated class.
*
* @author Christoph Strobl
* @since 1.10
*/
@Target({ ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(KeyValueRepositoriesRegistrar.class)
@QueryCreatorType(SpelQueryCreator.class)
public @interface EnableKeyValueRepositories {
/**
* Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
* {@code @EnableJpaRepositories("org.my.pkg")} instead of {@code @EnableJpaRepositories(basePackages="org.my.pkg")}.
*/
String[] value() default {};
/**
* Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this
* attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names.
*/
String[] basePackages() default {};
/**
* Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The
* package of each class specified will be scanned. Consider creating a special no-op marker class or interface in
* each package that serves no purpose other than being referenced by this attribute.
*/
Class<?>[] basePackageClasses() default {};
/**
* Specifies which types are not eligible for component scanning.
*/
Filter[] excludeFilters() default {};
/**
* Specifies which types are eligible for component scanning. Further narrows the set of candidate components from
* everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters.
*/
Filter[] includeFilters() default {};
/**
* Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So
* for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning
* for {@code PersonRepositoryImpl}.
*
* @return
*/
String repositoryImplementationPostfix() default "Impl";
/**
* Configures the location of where to find the Spring Data named queries properties file.
*
* @return
*/
String namedQueriesLocation() default "";
/**
* Returns the key of the {@link QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
* {@link Key#CREATE_IF_NOT_FOUND}.
*
* @return
*/
Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;
/**
* Returns the {@link FactoryBean} class to be used for each repository instance. Defaults to
* {@link KeyValueRepositoryFactoryBean}.
*
* @return
*/
Class<?> repositoryFactoryBeanClass() default KeyValueRepositoryFactoryBean.class;
/**
* Configures the name of the {@link KeyValueOperations} bean to be used with the repositories detected.
*
* @return
*/
String keyValueTemplateRef() default "keyValueTemplate";
/**
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
* repositories infrastructure.
*/
boolean considerNestedRepositories() default false;
}

View File

@@ -1,48 +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.config;
import java.lang.annotation.Annotation;
import org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
/**
* KeyValue specific {@link org.springframework.context.annotation.ImportBeanDefinitionRegistrar}
*
* @author Christoph Strobl
* @since 1.10
*/
public class KeyValueRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getAnnotation()
*/
@Override
protected Class<? extends Annotation> getAnnotation() {
return EnableKeyValueRepositories.class;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getExtension()
*/
@Override
protected RepositoryConfigurationExtension getExtension() {
return new KeyValueRepositoryConfigurationExtension();
}
}

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;
}
}