DATAVK-86 - Improved auto-template-registration infrastructure.
KeyValueRepositoryConfigurationExtension now need to report the default value for the KeyValueTemplate reference. We now only register a default template if the reference has not been customized. Changed the default reference value for Maps to mapKeyValueTemplate to allow individual defaults per store. Removed obsolete RepositoryNamespaceHandler. Original pull request: #1.
This commit is contained in:
@@ -32,7 +32,6 @@ 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}.
|
||||
@@ -40,11 +39,10 @@ import org.springframework.util.ObjectUtils;
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class KeyValueRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
|
||||
public abstract class KeyValueRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
|
||||
|
||||
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";
|
||||
protected static final String KEY_VALUE_TEMPLATE_BEAN_REF_ATTRIBUTE = "keyValueTemplateRef";
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
@@ -91,8 +89,7 @@ public class KeyValueRepositoryConfigurationExtension extends RepositoryConfigur
|
||||
|
||||
AnnotationAttributes attributes = config.getAttributes();
|
||||
|
||||
builder.addPropertyReference("keyValueOperations",
|
||||
attributes.getString(Key_VALUE_TEMPLATE_BEAN_NAME_CONFIG_ATTRIBUTE));
|
||||
builder.addPropertyReference("keyValueOperations", attributes.getString(KEY_VALUE_TEMPLATE_BEAN_REF_ATTRIBUTE));
|
||||
builder.addPropertyValue("queryCreator", getQueryCreatorType(config));
|
||||
builder.addPropertyReference("mappingContext", MAPPING_CONTEXT_BEAN_NAME);
|
||||
}
|
||||
@@ -127,33 +124,22 @@ public class KeyValueRepositoryConfigurationExtension extends RepositoryConfigur
|
||||
|
||||
super.registerBeansForRoot(registry, configurationSource);
|
||||
|
||||
potentiallyRegisterMappingContext(registry, configurationSource);
|
||||
potentiallyRegisterKeyValueTemplate(registry, configurationSource);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
String keyValueTemplateName = configurationSource.getAttribute(KEY_VALUE_TEMPLATE_BEAN_REF_ATTRIBUTE);
|
||||
|
||||
// No custom template reference configured
|
||||
if (getDefaultKeyValueTemplateRef().equals(keyValueTemplateName)) {
|
||||
|
||||
RootBeanDefinition beanDefinition = getDefaultKeyValueTemplateBeanDefinition();
|
||||
|
||||
if (beanDefinition != null) {
|
||||
registerIfNotAlreadyRegistered(beanDefinition, registry, keyValueTemplateName, configurationSource.getSource());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,4 +150,6 @@ public class KeyValueRepositoryConfigurationExtension extends RepositoryConfigur
|
||||
protected RootBeanDefinition getDefaultKeyValueTemplateBeanDefinition() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected abstract String getDefaultKeyValueTemplateRef();
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 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 org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.NamespaceHandler;
|
||||
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
|
||||
import org.springframework.data.repository.config.RepositoryBeanDefinitionParser;
|
||||
|
||||
/**
|
||||
* {@link NamespaceHandler} to register {@link BeanDefinitionParser}s for key-value repositories.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Christoph Strobl
|
||||
* @since 1.4
|
||||
*/
|
||||
public class RepositoryNameSpaceHandler extends NamespaceHandlerSupport {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.xml.NamespaceHandler#init()
|
||||
*/
|
||||
public void init() {
|
||||
|
||||
KeyValueRepositoryConfigurationExtension extension = new KeyValueRepositoryConfigurationExtension();
|
||||
RepositoryBeanDefinitionParser repositoryBeanDefinitionParser = new RepositoryBeanDefinitionParser(extension);
|
||||
registerBeanDefinitionParser("repositories", repositoryBeanDefinitionParser);
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ public @interface EnableMapRepositories {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
String keyValueTemplateRef() default "keyValueTemplate";
|
||||
String keyValueTemplateRef() default "mapKeyValueTemplate";
|
||||
|
||||
/**
|
||||
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
|
||||
|
||||
@@ -44,6 +44,15 @@ public class MapRepositoryConfigurationExtension extends KeyValueRepositoryConfi
|
||||
return "map";
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension#getDefaultKeyValueTemplateRef()
|
||||
*/
|
||||
@Override
|
||||
protected String getDefaultKeyValueTemplateRef() {
|
||||
return "mapKeyValueTemplate";
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension#getDefaultKeyValueTemplateBeanDefinition()
|
||||
|
||||
Reference in New Issue
Block a user