Spring BeanPostProcessor -- PersistenceExceptionTranslationPostProcessor

概述

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);
	}

}

参考文章

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值