DATACMNS-12 - Refactored transaction support of RepositoryFactoryBeanSupport into separate subclass.
As we will deal with data stores not supporting transactions we should not activate transactions by default. For persistence technologies providing transaction support there's TransactionalRepositoryFactoryBeanSupport now, which pretty much contains the functionality that formerly resided in RFBS. Note this required changes in the namespace as well. Concrete persistence technology specific namespaces now have to choose between repository-attributes or transactional-repository-attributes attribute group whereas the latter contains the transaction-manager-ref attribute.
This commit is contained in:
@@ -43,6 +43,7 @@ import org.springframework.core.type.filter.AssignableTypeFilter;
|
||||
import org.springframework.core.type.filter.RegexPatternTypeFilter;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
|
||||
@@ -205,6 +206,13 @@ public abstract class AbstractRepositoryConfigDefinitionParser<S extends GlobalR
|
||||
builder.addPropertyValue("queryLookupStrategyKey",
|
||||
context.getQueryLookupStrategyKey());
|
||||
|
||||
String transactionManagerRef = context.getTransactionManagerRef();
|
||||
|
||||
if (StringUtils.hasText(transactionManagerRef)) {
|
||||
builder.addPropertyValue("transactionManager",
|
||||
transactionManagerRef);
|
||||
}
|
||||
|
||||
String customImplementationBeanName =
|
||||
registerCustomImplementation(context, parser, beanSource);
|
||||
|
||||
|
||||
@@ -62,7 +62,8 @@ public interface CommonRepositoryConfigInformation {
|
||||
|
||||
/**
|
||||
* Returns the bean name of the {@link PlatformTransactionManager} to be
|
||||
* used.
|
||||
* used. Returns {@literal null} if no reference has been configured
|
||||
* explicitly.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
|
||||
@@ -144,8 +144,14 @@ public class ManualRepositoryConfigInformation<T extends CommonRepositoryConfigI
|
||||
protected String getAttribute(String attribute) {
|
||||
|
||||
String value = getSource().getAttribute(attribute);
|
||||
return hasText(value) ? value : getParent().getSource().getAttribute(
|
||||
attribute);
|
||||
|
||||
if (hasText(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
value = getParent().getSource().getAttribute(attribute);
|
||||
|
||||
return hasText(value) ? value : null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.data.repository.util.TxUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
@@ -167,8 +166,7 @@ public abstract class RepositoryConfig<T extends SingleRepositoryConfigInformati
|
||||
public String getTransactionManagerRef() {
|
||||
|
||||
String ref = element.getAttribute(TRANSACTION_MANAGER_REF);
|
||||
return StringUtils.hasText(ref) ? ref
|
||||
: TxUtils.DEFAULT_TRANSACTION_MANAGER;
|
||||
return StringUtils.hasText(ref) ? ref : null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -15,15 +15,15 @@
|
||||
*/
|
||||
package org.springframework.data.repository.support;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.annotation.Required;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.data.repository.util.TxUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ import org.springframework.util.Assert;
|
||||
* @param <T> the type of the repository
|
||||
*/
|
||||
public abstract class RepositoryFactoryBeanSupport<T extends Repository<?, ?>>
|
||||
implements FactoryBean<T>, InitializingBean, BeanFactoryAware {
|
||||
implements FactoryBean<T>, InitializingBean {
|
||||
|
||||
private RepositoryFactorySupport factory;
|
||||
|
||||
@@ -43,9 +43,6 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<?, ?>>
|
||||
private Class<? extends T> repositoryInterface;
|
||||
private Object customImplementation;
|
||||
|
||||
private String transactionManagerName = TxUtils.DEFAULT_TRANSACTION_MANAGER;
|
||||
private RepositoryProxyPostProcessor txPostProcessor;
|
||||
|
||||
|
||||
/**
|
||||
* Setter to inject the repository interface to implement.
|
||||
@@ -60,29 +57,17 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<?, ?>>
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the {@link QueryLookupStrategy.Key} to be used.
|
||||
*
|
||||
* @param queryLookupStrategyKey
|
||||
*/
|
||||
public void setQueryLookupStrategyKey(Key queryLookupStrategyKey) {
|
||||
|
||||
this.queryLookupStrategyKey = queryLookupStrategyKey;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setter to configure which transaction manager to be used. We have to use
|
||||
* the bean name explicitly as otherwise the qualifier of the
|
||||
* {@link org.springframework.transaction.annotation.Transactional}
|
||||
* annotation is used. By explicitly defining the transaction manager bean
|
||||
* name we favour let this one be the default one chosen.
|
||||
*
|
||||
* @param transactionManager
|
||||
*/
|
||||
public void setTransactionManager(String transactionManager) {
|
||||
|
||||
this.transactionManagerName =
|
||||
transactionManager == null ? TxUtils.DEFAULT_TRANSACTION_MANAGER
|
||||
: transactionManager;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setter to inject a custom repository implementation.
|
||||
*
|
||||
@@ -140,7 +125,23 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<?, ?>>
|
||||
this.factory = createRepositoryFactory();
|
||||
this.factory.setQueryLookupStrategyKey(queryLookupStrategyKey);
|
||||
this.factory.validate(repositoryInterface, customImplementation);
|
||||
this.factory.addRepositoryProxyPostProcessor(txPostProcessor);
|
||||
|
||||
for (RepositoryProxyPostProcessor processor : getRepositoryPostProcessors()) {
|
||||
this.factory.addRepositoryProxyPostProcessor(processor);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns all {@link RepositoryProxyPostProcessor} to be added to the
|
||||
* repository factory to be created. Default implementation will return an
|
||||
* empty list.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected List<RepositoryProxyPostProcessor> getRepositoryPostProcessors() {
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
@@ -150,22 +151,4 @@ public abstract class RepositoryFactoryBeanSupport<T extends Repository<?, ?>>
|
||||
* @return
|
||||
*/
|
||||
protected abstract RepositoryFactorySupport createRepositoryFactory();
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org
|
||||
* .springframework.beans.factory.BeanFactory)
|
||||
*/
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
|
||||
Assert.isInstanceOf(ListableBeanFactory.class, beanFactory);
|
||||
|
||||
this.txPostProcessor =
|
||||
new TransactionalRepositoryProxyPostProcessor(
|
||||
(ListableBeanFactory) beanFactory,
|
||||
transactionManagerName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2008-2010 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.repository.support;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.util.TxUtils;
|
||||
import org.springframework.transaction.interceptor.TransactionInterceptor;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
||||
/**
|
||||
* Extension of {@link RepositoryFactoryBeanSupport} to add transactional
|
||||
* capabilities to the repository proxy. Will register a
|
||||
* {@link TransactionalRepositoryProxyPostProcessor} that in turn adds a
|
||||
* {@link TransactionInterceptor} to the repository proxy to be created.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Repository<?, ?>>
|
||||
extends RepositoryFactoryBeanSupport<T> implements BeanFactoryAware {
|
||||
|
||||
private String transactionManagerName = TxUtils.DEFAULT_TRANSACTION_MANAGER;
|
||||
private RepositoryProxyPostProcessor txPostProcessor;
|
||||
|
||||
|
||||
/**
|
||||
* Setter to configure which transaction manager to be used. We have to use
|
||||
* the bean name explicitly as otherwise the qualifier of the
|
||||
* {@link org.springframework.transaction.annotation.Transactional}
|
||||
* annotation is used. By explicitly defining the transaction manager bean
|
||||
* name we favour let this one be the default one chosen.
|
||||
*
|
||||
* @param transactionManager
|
||||
*/
|
||||
public void setTransactionManager(String transactionManager) {
|
||||
|
||||
this.transactionManagerName =
|
||||
transactionManager == null ? TxUtils.DEFAULT_TRANSACTION_MANAGER
|
||||
: transactionManager;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.support.RepositoryFactoryBeanSupport
|
||||
* #getRepositoryPostProcessors()
|
||||
*/
|
||||
@Override
|
||||
public List<RepositoryProxyPostProcessor> getRepositoryPostProcessors() {
|
||||
|
||||
return Arrays.asList(txPostProcessor);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org
|
||||
* .springframework.beans.factory.BeanFactory)
|
||||
*/
|
||||
public void setBeanFactory(BeanFactory beanFactory) {
|
||||
|
||||
Assert.isInstanceOf(ListableBeanFactory.class, beanFactory);
|
||||
|
||||
this.txPostProcessor =
|
||||
new TransactionalRepositoryProxyPostProcessor(
|
||||
(ListableBeanFactory) beanFactory,
|
||||
transactionManagerName);
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,6 @@
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attributeGroup ref="repository-attributes" />
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:complexType name="repository">
|
||||
@@ -50,13 +49,16 @@
|
||||
</xsd:annotation>
|
||||
<xsd:attribute name="id" type="xsd:string" use="required" />
|
||||
<xsd:attribute name="custom-impl-ref" type="customImplementationReference" />
|
||||
<xsd:attributeGroup ref="repository-attributes" />
|
||||
</xsd:complexType>
|
||||
|
||||
<xsd:attributeGroup name="repository-attributes">
|
||||
<xsd:attribute name="repository-impl-postfix" type="xsd:string" />
|
||||
<xsd:attribute name="query-lookup-strategy" type="query-strategy" />
|
||||
<xsd:attribute name="factory-class" type="classType" />
|
||||
</xsd:attributeGroup>
|
||||
|
||||
<xsd:attributeGroup name="transactional-repository-attributes">
|
||||
<xsd:attributeGroup ref="repository-attributes" />
|
||||
<xsd:attribute name="transaction-manager-ref" type="transactionManagerRef" />
|
||||
</xsd:attributeGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user