关于java:Transaction注解原理

6次阅读

共计 3457 个字符,预计需要花费 9 分钟才能阅读完成。

开启事务注解 EnableTransactionManagement,该注解往容器中导入了导入 TransactionManagementConfigurationSelector 组件。该组件有个办法,在容器刷新的时候会被调用。(此处不解说为什么会被调用,重点解说 Transaction 注解)
protected String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
case PROXY:

        return new String[] {AutoProxyRegistrar.class.getName(),
                ProxyTransactionManagementConfiguration.class.getName()};
    case ASPECTJ:
            return new String[] {

TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME};
 default:
 return null;
 }
 }

EnableTransactionManagement 默认是 AdviceMode mode() default AdviceMode.PROXY; 所以
AutoProxyRegistrar 和 ProxyTransactionManagementConfiguration 会被加载到容器中。

AutoProxyRegistrar 性能是往容器中注册了 InfrastructureAdvisorAutoProxyCreator 是个 BeanPostProcessor 后置处理器。
ProxyTransactionManagementConfiguration 利用 @Bean 注解往容器中增加多个 bean。(BeanFactoryTransactionAttributeSourceAdvisor 是个 Advisor 和后面 Aop 中 @Before 一样都是告诉办法,然而此处的是手动导入的,Aop 是本人主动生成的)。

在 bean 生成实现当前会调用后置处理器初始化。会去查找 bean 有没有适合的告诉办法。所以重点是在找告诉办法,找到了适合的就会生成代理对象。

// 简略的剖析一下流程
AbstractAutowireCapableBeanFactory#initializeBean() ->
AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization() ->
AbstractAutoProxyCreator#postProcessAfterInitialization() ->
AbstractAutoProxyCreator#wrapIfNecessary()->
AbstractAdvisorAutoProxyCreator#getAdvicesAndAdvisorsForBean()
/**
 * 寻找可用的告诉办法
 */
 protected List<Advisor> findEligibleAdvisors(Class<?> beanClass, String beanName) {
 // 寻找所有的 Advisors, 会去容器遍历所有类型为 Advisor 的 bean,
 //BeanFactoryTransactionAttributeSourceAdvisor 就是咱们手动导入的 Advisor
 List<Advisor> candidateAdvisors = findCandidateAdvisors();
 // 在所有的 Advisors, 看是否有 beanClass 匹配的
 List<Advisor> eligibleAdvisors = findAdvisorsThatCanApply(candidateAdvisors, beanClass, beanName);
 extendAdvisors(eligibleAdvisors);
 if (!eligibleAdvisors.isEmpty()) {
 eligibleAdvisors = sortAdvisors(eligibleAdvisors);
 }
 return eligibleAdvisors;
 }
// 找到所有的 Advisors, 剩下就是是否有匹配的 findAdvisorsThatCanApply() 办法 // 最终走到 TransactionAttributeSourcePointcut#matches//->AbstractFallbackTransactionAttributeSource#computeTransactionAttribute

`javaprotected TransactionAttribute computeTransactionAttribute(Method method, Class<?> targetClass) {
 // 判断办法修饰符是不是 Public,allowPublicMethodsOnly()默认是 true 的
 if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
 return null;
 }

 // 这外面就是判断办法上有没有 Transactional 注解,有进行解析操作并返回。
 TransactionAttribute txAttr = findTransactionAttribute(specificMethod);
 if (txAttr != null) {
 return txAttr;
 }
 // 代码每贴全,为了少点没用的货色。。。。
那要是找了适合的告诉办法,就生成代理对象,并设置回调函数。设置回调函数是 CglibAopProxy.DynamicAdvisedInterceptor 的 intercept 办法,该办法有个 getInterceptorsAndDynamicInterceptionAdvice 获取适合的拦截器。(事务的拦截器是 TransactionInterceptor)
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
 // 获取指标对象,不是代理对象
 target = getTarget();
 if (target != null) {
 targetClass = target.getClass();
 }
 // 获取办法适合的拦截器
 List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
 Object retVal;
 // 没有适合的拦截器并且办法的修饰符是 Public
 if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
 Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
 // 会调用指标办法
 retVal = methodProxy.invoke(target, argsToUse);
 }
 else {
 /*
 * 1. 有适合的拦截器: 跟单网 www.gendan5.com 会去执行拦截器 TransactionInterceptor#invoke(),
 * 事务的提交,回滚等一系列操作都在外面。具体是调去了
 * TransactionAspectSupport#invokeWithinTransaction() 办法
 * 2. 没有适合的拦截器并且办法的修饰符不是 Public: 这外面也会去调指标办法
 * (这里简略能够走进去看一下)
 */
 retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
 }
 retVal = processReturnType(proxy, target, method, retVal);
 return retVal;
 }

正文完
 0