DATACMNS-656 - Make application of default transactions configurable on TrasnactionalRepositoryFactoryBeanSupport.

TrasnactionalRepositoryFactoryBeanSupport now exposes a setEnableDefaultTransactions(…) which, if disabled, causes the TransactionalRepositoryProxyPostProcessor not to be registered and thus no default transactions to be applied.

Related tickets: DATAJPA-685.
This commit is contained in:
Oliver Gierke
2015-03-10 19:11:06 +01:00
parent 57180568c5
commit d7adfbd859
4 changed files with 105 additions and 4 deletions

View File

@@ -134,7 +134,7 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware {
*/
public void addRepositoryProxyPostProcessor(RepositoryProxyPostProcessor processor) {
Assert.notNull(processor);
Assert.notNull(processor, "RepositoryProxyPostProcessor must not be null!");
this.postProcessors.add(processor);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2013 the original author or authors.
* Copyright 2008-2015 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.
@@ -38,6 +38,7 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
private String transactionManagerName = TxUtils.DEFAULT_TRANSACTION_MANAGER;
private RepositoryProxyPostProcessor txPostProcessor;
private RepositoryProxyPostProcessor exceptionPostProcessor;
private boolean enableDefaultTransactions = true;
/**
* Setter to configure which transaction manager to be used. We have to use the bean name explicitly as otherwise the
@@ -50,6 +51,15 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
this.transactionManagerName = transactionManager == null ? TxUtils.DEFAULT_TRANSACTION_MANAGER : transactionManager;
}
/**
* Configures whether to enable the default transactions configured at the repository base implementation class.
*
* @param enableDefaultTransactions the enableDefaultTransactions to set
*/
public void setEnableDefaultTransactions(boolean enableDefaultTransactions) {
this.enableDefaultTransactions = enableDefaultTransactions;
}
/**
* Delegates {@link RepositoryFactorySupport} creation to {@link #doCreateRepositoryFactory()} and applies the
* {@link TransactionalRepositoryProxyPostProcessor} to the created instance.
@@ -61,7 +71,11 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
RepositoryFactorySupport factory = doCreateRepositoryFactory();
factory.addRepositoryProxyPostProcessor(exceptionPostProcessor);
factory.addRepositoryProxyPostProcessor(txPostProcessor);
if (enableDefaultTransactions) {
factory.addRepositoryProxyPostProcessor(txPostProcessor);
}
return factory;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011-2014 the original author or authors.
* Copyright 2011-2015 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.
@@ -27,7 +27,9 @@ import java.util.Set;
import java.util.concurrent.Future;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
@@ -58,6 +60,8 @@ import org.springframework.util.ClassUtils;
@RunWith(MockitoJUnitRunner.class)
public class RepositoryFactorySupportUnitTests {
public @Rule ExpectedException exception = ExpectedException.none();
DummyRepositoryFactory factory;
@Mock PagingAndSortingRepository<Object, Serializable> backingRepo;
@@ -207,6 +211,18 @@ public class RepositoryFactorySupportUnitTests {
assertThat(result.iterator().next(), is((Object) "Dave"));
}
/**
* @see DATACMNS-656
*/
@Test
public void rejectsNullRepositoryProxyPostProcessor() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage(RepositoryProxyPostProcessor.class.getSimpleName());
factory.addRepositoryProxyPostProcessor(null);
}
interface ObjectRepository extends Repository<Object, Serializable>, ObjectRepositoryCustom {
Object findByClass(Class<?> clazz);

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2015 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.core.support;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.interceptor.TransactionInterceptor;
/**
* Unit tests for {@link TransactionalRepositoryFactoryBeanSupport}.
*
* @author Oliver Gierke
* @soundtrack The Intersphere - Live in Mannheim
*/
public class TransactionRepositoryFactoryBeanSupportUnitTests {
/**
* @see DATACMNS-656
*/
@Test
public void doesNotRegisterTransactionalRepositoryProxyPostProcessorIfConfigured() {
SampleTransactionalRepositoryFactoryBean factoryBean = new SampleTransactionalRepositoryFactoryBean();
factoryBean.setEnableDefaultTransactions(false);
factoryBean.setBeanFactory(new DefaultListableBeanFactory());
factoryBean.afterPropertiesSet();
CrudRepository<Object, Long> repository = factoryBean.getObject();
Advisor[] advisors = ((Advised) repository).getAdvisors();
assertThat(advisors.length, is(greaterThanOrEqualTo(2)));
assertThat(advisors[1].getAdvice(), is(not(instanceOf(TransactionInterceptor.class))));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
static class SampleTransactionalRepositoryFactoryBean extends
TransactionalRepositoryFactoryBeanSupport<CrudRepository<Object, Long>, Object, Long> {
private final CrudRepository<Object, Long> repository = mock(CrudRepository.class);
public SampleTransactionalRepositoryFactoryBean() {
setRepositoryInterface((Class) CrudRepository.class);
}
@Override
protected RepositoryFactorySupport doCreateRepositoryFactory() {
return new DummyRepositoryFactory(repository);
}
}
}