From 467283325a1e449f7f24277d91bf73653e5ab9ac Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 26 Jan 2011 09:57:11 +0200 Subject: [PATCH] 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. --- ...tractRepositoryConfigDefinitionParser.java | 8 ++ .../CommonRepositoryConfigInformation.java | 3 +- .../ManualRepositoryConfigInformation.java | 10 +- .../repository/config/RepositoryConfig.java | 4 +- .../support/RepositoryFactoryBeanSupport.java | 71 ++++++-------- ...sactionalRepositoryFactoryBeanSupport.java | 92 +++++++++++++++++++ .../config/spring-repository-1.0.xsd | 6 +- 7 files changed, 142 insertions(+), 52 deletions(-) create mode 100644 spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryFactoryBeanSupport.java diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AbstractRepositoryConfigDefinitionParser.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AbstractRepositoryConfigDefinitionParser.java index 70ed9ce5f..dd9b157d5 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AbstractRepositoryConfigDefinitionParser.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AbstractRepositoryConfigDefinitionParser.java @@ -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 the type of the repository */ public abstract class RepositoryFactoryBeanSupport> - implements FactoryBean, InitializingBean, BeanFactoryAware { + implements FactoryBean, InitializingBean { private RepositoryFactorySupport factory; @@ -43,9 +43,6 @@ public abstract class RepositoryFactoryBeanSupport> private Class 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> } + /** + * 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> 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 getRepositoryPostProcessors() { + + return Collections.emptyList(); } @@ -150,22 +151,4 @@ public abstract class RepositoryFactoryBeanSupport> * @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); - } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryFactoryBeanSupport.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryFactoryBeanSupport.java new file mode 100644 index 000000000..4f2df71b2 --- /dev/null +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryFactoryBeanSupport.java @@ -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> + extends RepositoryFactoryBeanSupport 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 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); + } +} diff --git a/spring-data-commons-core/src/main/resources/org/springframework/data/repository/config/spring-repository-1.0.xsd b/spring-data-commons-core/src/main/resources/org/springframework/data/repository/config/spring-repository-1.0.xsd index cea1bab0d..ec2fb81df 100644 --- a/spring-data-commons-core/src/main/resources/org/springframework/data/repository/config/spring-repository-1.0.xsd +++ b/spring-data-commons-core/src/main/resources/org/springframework/data/repository/config/spring-repository-1.0.xsd @@ -34,7 +34,6 @@ ]]> - @@ -50,13 +49,16 @@ - + + + +