DATACMNS-318 - Refined persistence exception translation infrastructure.
Extracted the activation of persistence exception translation into a separate RepositoryProxyPostProcessor. Adapted TransactionalRepositoryFactoryBeanSupport to also register the newly introduced PersistenceExceptionTranslationRepositoryProxyPostProcessor.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2013 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 org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link RepositoryProxyPostProcessor} to register a {@link PersistenceExceptionTranslationInterceptor} on the
|
||||
* repository proxy.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class PersistenceExceptionTranslationRepositoryProxyPostProcessor implements RepositoryProxyPostProcessor {
|
||||
|
||||
private final PersistenceExceptionTranslationInterceptor interceptor;
|
||||
|
||||
/**
|
||||
* Creates a new {@link PersistenceExceptionTranslationRepositoryProxyPostProcessor} using the given
|
||||
* {@link ListableBeanFactory}.
|
||||
*
|
||||
* @param beanFactory must not be {@literal null}.
|
||||
*/
|
||||
public PersistenceExceptionTranslationRepositoryProxyPostProcessor(ListableBeanFactory beanFactory) {
|
||||
|
||||
Assert.notNull(beanFactory, "BeanFactory must not be null!");
|
||||
|
||||
this.interceptor = new PersistenceExceptionTranslationInterceptor();
|
||||
this.interceptor.setBeanFactory(beanFactory);
|
||||
this.interceptor.afterPropertiesSet();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.core.support.RepositoryProxyPostProcessor#postProcess(org.springframework.aop.framework.ProxyFactory)
|
||||
*/
|
||||
public void postProcess(ProxyFactory factory) {
|
||||
factory.addAdvice(interceptor);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2010 the original author or authors.
|
||||
* Copyright 2008-2013 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.
|
||||
@@ -37,6 +37,7 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
|
||||
|
||||
private String transactionManagerName = TxUtils.DEFAULT_TRANSACTION_MANAGER;
|
||||
private RepositoryProxyPostProcessor txPostProcessor;
|
||||
private RepositoryProxyPostProcessor exceptionPostProcessor;
|
||||
|
||||
/**
|
||||
* Setter to configure which transaction manager to be used. We have to use the bean name explicitly as otherwise the
|
||||
@@ -46,7 +47,6 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
|
||||
* @param transactionManager
|
||||
*/
|
||||
public void setTransactionManager(String transactionManager) {
|
||||
|
||||
this.transactionManagerName = transactionManager == null ? TxUtils.DEFAULT_TRANSACTION_MANAGER : transactionManager;
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
|
||||
protected final RepositoryFactorySupport createRepositoryFactory() {
|
||||
|
||||
RepositoryFactorySupport factory = doCreateRepositoryFactory();
|
||||
factory.addRepositoryProxyPostProcessor(exceptionPostProcessor);
|
||||
factory.addRepositoryProxyPostProcessor(txPostProcessor);
|
||||
return factory;
|
||||
}
|
||||
@@ -72,17 +73,15 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
|
||||
protected abstract RepositoryFactorySupport doCreateRepositoryFactory();
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org
|
||||
* .springframework.beans.factory.BeanFactory)
|
||||
*/
|
||||
* (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);
|
||||
ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
|
||||
this.txPostProcessor = new TransactionalRepositoryProxyPostProcessor(listableBeanFactory, transactionManagerName);
|
||||
this.exceptionPostProcessor = new PersistenceExceptionTranslationRepositoryProxyPostProcessor(listableBeanFactory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2010 the original author or authors.
|
||||
* Copyright 2008-2013 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.
|
||||
@@ -53,20 +53,19 @@ import org.springframework.util.ObjectUtils;
|
||||
class TransactionalRepositoryProxyPostProcessor implements RepositoryProxyPostProcessor {
|
||||
|
||||
private final TransactionInterceptor transactionInterceptor;
|
||||
private final PersistenceExceptionTranslationInterceptor petInterceptor;
|
||||
|
||||
/**
|
||||
* Creates a new {@link TransactionalRepositoryProxyPostProcessor}.
|
||||
* Creates a new {@link TransactionalRepositoryProxyPostProcessor} using the given {@link ListableBeanFactory} and
|
||||
* transaction manager bean name.
|
||||
*
|
||||
* @param beanFactory must not be {@literal null}.
|
||||
* @param transactionManagerName must not be {@literal null} or empty.
|
||||
*/
|
||||
public TransactionalRepositoryProxyPostProcessor(ListableBeanFactory beanFactory, String transactionManagerName) {
|
||||
|
||||
Assert.notNull(beanFactory);
|
||||
Assert.notNull(transactionManagerName);
|
||||
|
||||
this.petInterceptor = new PersistenceExceptionTranslationInterceptor();
|
||||
this.petInterceptor.setBeanFactory(beanFactory);
|
||||
this.petInterceptor.afterPropertiesSet();
|
||||
|
||||
this.transactionInterceptor = new TransactionInterceptor(null, new CustomAnnotationTransactionAttributeSource());
|
||||
this.transactionInterceptor.setTransactionManagerBeanName(transactionManagerName);
|
||||
this.transactionInterceptor.setBeanFactory(beanFactory);
|
||||
@@ -74,15 +73,10 @@ class TransactionalRepositoryProxyPostProcessor implements RepositoryProxyPostPr
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.support.RepositoryProxyPostProcessor
|
||||
* #postProcess(org.springframework.aop.framework.ProxyFactory)
|
||||
*/
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.core.support.RepositoryProxyPostProcessor#postProcess(org.springframework.aop.framework.ProxyFactory)
|
||||
*/
|
||||
public void postProcess(ProxyFactory factory) {
|
||||
|
||||
factory.addAdvice(petInterceptor);
|
||||
factory.addAdvice(transactionInterceptor);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user