概述
PersistenceExceptionTranslationPostProcessor是一个BeanPostProcessor,继承自AbstractBeanFactoryAwareAdvisingPostProcessor,它持有一个PersistenceExceptionTranslationAdvisor对象,在每个bean初始化后置处理阶段检测该bean是否符合包裹该PersistenceExceptionTranslationAdvisor的条件,如果符合,则包裹该Advisor到这个bean。具体的包裹方案,参考基类AbstractBeanFactoryAwareAdvisingPostProcessor的逻辑实现。
PersistenceExceptionTranslationPostProcessor包裹PersistenceExceptionTranslationAdvisor到目标bean的话,是包裹到最里面(如果目标bean上已经存在其他Advisor,则PersistenceExceptionTranslationAdvisor则包裹在这些Advisor的里面,距离目标bean最近)。
关于
PersistenceExceptionTranslationPostProcessor的使用,可以参考 :
Spring Boot 自动配置 : PersistenceExceptionTranslationAutoConfiguration
源代码解析
源代码版本 : spring-tx-5.1.5.RELEASE
package org.springframework.dao.annotation;
import java.lang.annotation.Annotation;
import org.springframework.aop.framework.autoproxy.AbstractBeanFactoryAwareAdvisingPostProcessor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
@SuppressWarnings("serial")
public class PersistenceExceptionTranslationPostProcessor
extends AbstractBeanFactoryAwareAdvisingPostProcessor {
private Class<? extends Annotation> repositoryAnnotationType = Repository.class;
/**
* Set the 'repository' annotation type.
* The default repository annotation type is the Repository annotation.
* This setter property exists so that developers can provide their own
* (non-Spring-specific) annotation type to indicate that a class has a
* repository role.
* @param repositoryAnnotationType the desired annotation type
*/
public void setRepositoryAnnotationType(Class<? extends Annotation> repositoryAnnotationType) {
Assert.notNull(repositoryAnnotationType, "'repositoryAnnotationType' must not be null");
this.repositoryAnnotationType = repositoryAnnotationType;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
if (!(beanFactory instanceof ListableBeanFactory)) {
throw new IllegalArgumentException(
"Cannot use PersistenceExceptionTranslator autodetection without ListableBeanFactory");
}
this.advisor = new PersistenceExceptionTranslationAdvisor(
(ListableBeanFactory) beanFactory, this.repositoryAnnotationType);
}
}

3227

被折叠的 条评论
为什么被折叠?



