Spring源码-@EnableTransactionManagement方式事务实现分析(上)

 

先总览实现逻辑:

@EnableTransactionManagement使用 @Import方式,注入TransactionManagementConfigurationSelector的Bean,相当于间接注入了以下两个bean

    1、AutoProxyRegistrar

    实现了ImportBeanDefinitionRegistrar接口,在registerBeanDefinitions方法中,将InfrastructureAdvisorAutoProxyCreator注册名称为org.springframework.aop.config.internalAutoProxyCreator的bean。InfrastructureAdvisorAutoProxyCreator实现了AbstractAutoProxyCreator的getAdvicesAndAdvisorsForBean方法,在其中将含有@Transactional的进行后置增强。

    2、ProxyTransactionManagementConfiguration(其间接注入了三个bean)

    1)、TransactionAttributeSource(事务解析器)

    其实现为AnnotationTransactionAttributeSource,用于解析我们在@Transactional中配置的属性信息

    2)、TransactionInterceptor(事务方法拦截器)

    注入TransactionInterceptor时依赖注入了TransactionAttributeSource,所以拥有解析@Transactional的能力;通过ProxyTransactionManagementConfiguration实现的ImportAwear回调时导入了txManager,赋值给TransactionInterceptor。实现MethodInterceptor的invoke。根据优先级获取事务管理器、按照声明式事务和编程式事务(不进行分析了)进行处理。

        创建事务(如果必要,根据配置解析判断)

        try:执行事务增强的方法

        catch:异常进行处理(如果必要进行回滚)

        finally:清除事务信息

        提交事务,组装返回信息

    3)、BeanFactoryTransactionAttributeSourceAdvisor(事务增强器)

    依赖注入了TransactionAttributeSourceTransactionInterceptor两个bean,实现了Aop的getAdvisor,将注入的TransactionInterceptor返回。

 

所以分为六个进行分析,顺序依次为:

TransactionManagementConfigurationSelector、

AutoProxyRegistrar、

ProxyTransactionManagementConfiguration、

AnnotationTransactionAttributeSource、

TransactionInterceptor

BeanFactoryTransactionAttributeSourceAdvisor

一、  TransactionManagementConfigurationSelector分析

    我们知道当在Spring的Bean上添加注解,就可以开启事务,并且只要在bean的类或者方法上添加@Transactional就可以添加事务。先看看@EnableTransactionManagement结构:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {

	boolean proxyTargetClass() default false;

	AdviceMode mode() default AdviceMode.PROXY;

	int order() default Ordered.LOWEST_PRECEDENCE;
}

    1、将 TransactionManagementConfigurationSelector 注入为bean

    2、有三个属性 (ImportSelector回调时使用)

proxyTargetClass: 是否代理当前的类 (cglib使用子类继承方式实现)

mode:使用Aop的代理方式,默认使用jdk动态代理

order:Intercepter的排序

 

@Import(TransactionManagementConfigurationSelector.class), 很明显使用@Import 将其注入容器中。结构如下:

public class TransactionManagementConfigurationSelector extends 
    AdviceModeImportSelector<EnableTransactionManagement> {

    @Override
    protected String[] selectImports(AdviceMode adviceMode) {
        switch (adviceMode) {
            case PROXY:
                return new String[] {AutoProxyRegistrar.class.getName(),
						ProxyTransactionManagementConfiguration.class.getName()};
            case ASPECTJ:
                return new String[] {determineTransactionAspectClass()};
            default:
                return null;
        }
    }

    private String determineTransactionAspectClass() {
        return (ClassUtils.isPresent("javax.transaction.Transactional", 
            getClass().getClassLoader()) ?
        TransactionManagementConfigUtils.JTA_TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME :
		TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME);
    }

}

    又使用 ImportSelector的方式将返回的字符串类注入为Bean(想象可以参加:Spring源码-ImportSelector实现分析),到底注入什么,根据我们在启动类上注入的@EnableTransactionManagement的mode属性,在这里回传给我们进行判断。默认使用PROXY,即jdk的动态代理。则将会把 AutoProxyRegistrar 和 ProxyTransactionManagementConfiguration注为bean。如果是ASPECTJ则需要判断类加载器是否加载了javax.transaction.Transactional,即是否使用jta方式(org.springframework.transaction.aspectj.AspectJJtaTransactionManagementConfiguration或org.springframework.transaction.aspectj.AspectJTransactionManagementConfiguration)。一般都很少配置属性,使用默认的jdk代理方式,所以基于该方式进行分析。

    AutoProxyRegistrar用于注册一个自动代理创建者(APC)去发现可被代理的目标对象。

 

    ProxyTransactionManagementConfiguration结构比较复杂,如下:

@Configuration(proxyBeanMethods = false)
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {

	@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(
			TransactionAttributeSource transactionAttributeSource,
			TransactionInterceptor transactionInterceptor) {
		BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
		advisor.setTransactionAttributeSource(transactionAttributeSource);
		advisor.setAdvice(transactionInterceptor);
		if (this.enableTx != null) {
			advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
		}
		return advisor;
	}

	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public TransactionAttributeSource transactionAttributeSource() {
		return new AnnotationTransactionAttributeSource();
	}

	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public TransactionInterceptor transactionInterceptor(
			TransactionAttributeSource transactionAttributeSource) {
		TransactionInterceptor interceptor = new TransactionInterceptor();
		interceptor.setTransactionAttributeSource(transactionAttributeSource);
		if (this.txManager != null) {
			interceptor.setTransactionManager(this.txManager);
		}
		return interceptor;
	}

}

本类中注入了三个bean,基于依赖注入的原则,所以注册为BeanDefinition顺序为:TransactionAttributeSource、TransactionInterceptor和BeanFactoryTransactionAttributeSourceAdvisor。

二 、AutoProxyRegistrar分析

    AutoProxyRegistrar实现了接口ImportBeanDefinitionRegistrar,其回调方法registerBeanDefinitions如下:

public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    boolean candidateFound = false;
    Set<String> annTypes = importingClassMetadata.getAnnotationTypes();
    for (String annType : annTypes) {
        AnnotationAttributes candidate = AnnotationConfigUtils.attributesFor(importingClassMetadata, annType);
        if (candidate == null) {
            continue;
        }
        Object mode = candidate.get("mode");
        Object proxyTargetClass = candidate.get("proxyTargetClass");
        if (mode != null && proxyTargetClass != null && AdviceMode.class == mode.getClass() &&
                Boolean.class == proxyTargetClass.getClass()) {
            candidateFound = true;
            if (mode == AdviceMode.PROXY) {
                AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
                if ((Boolean) proxyTargetClass) {
                    AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
                    return;
                }
            }
        }
    }
    if (!candidateFound && logger.isInfoEnabled()) {
        String name = getClass().getSimpleName();
        logger.info(String.format("%s was imported but no annotations were found " +
                "having both 'mode' and 'proxyTargetClass' attributes of type " +
                "AdviceMode and boolean respectively. This means that auto proxy " +
                "creator registration and configuration may not have occurred as " +
                "intended, and components may not be proxied as expected. Check to " +
                "ensure that %s has been @Import'ed on the same class where these " +
                "annotations are declared; otherwise remove the import of %s " +
                "altogether.", name, name, name));
    }
}

获取所以的注解的类进行遍历,获取每一个注解的注解属性,如果获取到@EnableTransactionManagement上的mode和proxyTargetClass属性,判断如果是jdk代理类型,则执行方法:

AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
public static BeanDefinition registerAutoProxyCreatorIfNecessary(
			BeanDefinitionRegistry registry, @Nullable Object source) {
    return registerOrEscalateApcAsRequired(
        InfrastructureAdvisorAutoProxyCreator.class, registry, source);
}
private static BeanDefinition registerOrEscalateApcAsRequired(
        Class<?> cls, BeanDefinitionRegistry registry, @Nullable Object source) {

    Assert.notNull(registry, "BeanDefinitionRegistry must not be null");

    if (registry.containsBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME)) {
        BeanDefinition apcDefinition = registry.getBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME);
        if (!cls.getName().equals(apcDefinition.getBeanClassName())) {
            int currentPriority = findPriorityForClass(apcDefinition.getBeanClassName());
            int requiredPriority = findPriorityForClass(cls);
            if (currentPriority < requiredPriority) {
                apcDefinition.setBeanClassName(cls.getName());
            }
        }
        return null;
    }

    RootBeanDefinition beanDefinition = new RootBeanDefinition(cls);
    beanDefinition.setSource(source);
    beanDefinition.getPropertyValues().add("order", Ordered.HIGHEST_PRECEDENCE);
    beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
    registry.registerBeanDefinition(AUTO_PROXY_CREATOR_BEAN_NAME, beanDefinition);
    return beanDefinition;
}

   之前出入的source为null,cls为InfrastructureAdvisorAutoProxyCreator.class。判断容器中是否注入名称为org.springframework.aop.config.internalAutoProxyCreator的bean,默认是没有。则或new一个RootBeanDefinition(cls),并且注册为该名称,即把InfrastructureAdvisorAutoProxyCreator注册成一个bean。

    后面判断,如果我们在@EnableTransactionMagagement上,配置为jdk,并且proxyTargetClass设置为true,则会在之前的RootBeanDefinition上添加属性:

definition.getPropertyValues().add("proxyTargetClass", Boolean.TRUE);

InfrastructureAdvisorAutoProxyCreator

那么相当于所以要做的事都委派给InfrastructureAdvisorAutoProxyCreator,其机构如下:

那么在每一个bean实例化的生命周期时都会回调该方法(postProcessAfterInitialization,继续追踪:

public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
    if (bean != null) {
        Object cacheKey = getCacheKey(bean.getClass(), beanName);
        if (this.earlyProxyReferences.remove(cacheKey) != bean) {
            return wrapIfNecessary(bean, beanName, cacheKey);
        }
    }
    return bean;
}

 每一个bean都先根据Class和bean的名称,在缓存中获取,否则创建并且放入缓存中。

protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
    if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
        return bean;
    }
    if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
        return bean;
    }
    if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
        this.advisedBeans.put(cacheKey, Boolean.FALSE);
        return bean;
    }

    // Create proxy if we have advice.
    Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
    if (specificInterceptors != DO_NOT_PROXY) {
        this.advisedBeans.put(cacheKey, Boolean.TRUE);
        Object proxy = createProxy(
                bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
        this.proxyTypes.put(cacheKey, proxy.getClass());
        return proxy;
    }

    this.advisedBeans.put(cacheKey, Boolean.FALSE);
    return bean;
}

1、先获取该bean需要哪些代理 

Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);

2、创建代理

Object proxy = createProxy(
                bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));

3、将代理对象放入缓存

this.proxyTypes.put(cacheKey, proxy.getClass()); 

 

二、AnnotationTransactionAttributeSource

TransactionInteceptor、BeanFactoryTransactionAttributeSourceAdvisor的bean初始化都有依赖了AnnotationTransactionAttributeSource,那么先解析其结构:

public class AnnotationTransactionAttributeSource extends 
    AbstractFallbackTransactionAttributeSource implements Serializable {

	private static final boolean jta12Present;

	private static final boolean ejb3Present;

	static {
		ClassLoader classLoader = AnnotationTransactionAttributeSource.class.getClassLoader();
		jta12Present = ClassUtils.isPresent("javax.transaction.Transactional", classLoader);
		ejb3Present = ClassUtils.isPresent("javax.ejb.TransactionAttribute", classLoader);
	}

	private final boolean publicMethodsOnly;

	private final Set<TransactionAnnotationParser> annotationParsers;

	public AnnotationTransactionAttributeSource() {
		this(true);
	}

	public AnnotationTransactionAttributeSource(boolean publicMethodsOnly) {
		this.publicMethodsOnly = publicMethodsOnly;
		if (jta12Present || ejb3Present) {
			this.annotationParsers = new LinkedHashSet<>(4);
			this.annotationParsers.add(new SpringTransactionAnnotationParser());
			if (jta12Present) {
				this.annotationParsers.add(new JtaTransactionAnnotationParser());
			}
			if (ejb3Present) {
				this.annotationParsers.add(new Ejb3TransactionAnnotationParser());
			}
		}
		else {
			this.annotationParsers = Collections.singleton(new SpringTransactionAnnotationParser());
		}
	}
}

使用无参数构造初始化,再调用了有参数构造,在静态代码块中根据类加载器判断是否加载了 jta和ejb,但是不管那种判断方式,都初始化了SpringTransactionAnnotationParser作为解析器。

实现了顶层接口的 getTransactionAttribute(Method method, @Nullable Class<?> targetClass),在父类AbstractFallbackTransactionAttributeSource中进行实现。就用于根据一个传入的Class和方法获取其@Transactional属性信息的功能。

public TransactionAttribute getTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
    if (method.getDeclaringClass() == Object.class) {
        return null;
    }

    // First, see if we have a cached value.
    Object cacheKey = getCacheKey(method, targetClass);
    TransactionAttribute cached = this.attributeCache.get(cacheKey);
    if (cached != null) {
        // Value will either be canonical value indicating there is no transaction attribute,
        // or an actual transaction attribute.
        if (cached == NULL_TRANSACTION_ATTRIBUTE) {
            return null;
        }
        else {
            return cached;
        }
    }
    else {
        // We need to work it out.
        TransactionAttribute txAttr = computeTransactionAttribute(method, targetClass);
        // Put it in the cache.
        if (txAttr == null) {
            this.attributeCache.put(cacheKey, NULL_TRANSACTION_ATTRIBUTE);
        }
        else {
            String methodIdentification = ClassUtils.getQualifiedMethodName(method, targetClass);
            if (txAttr instanceof DefaultTransactionAttribute) {
                ((DefaultTransactionAttribute) txAttr).setDescriptor(methodIdentification);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Adding transactional method '" + methodIdentification + "' with attribute: " + txAttr);
            }
            this.attributeCache.put(cacheKey, txAttr);
        }
        return txAttr;
    }
}

先判断缓存,否则进行解析再放入缓存。 TransactionAttribute txAttr = computeTransactionAttribute(method, targetClass);

@Nullable
protected TransactionAttribute computeTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
    // Don't allow no-public methods as required.
    if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
        return null;
    }

    // The method may be on an interface, but we need attributes from the target class.
    // If the target class is null, the method will be unchanged.
    Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);

    // First try is the method in the target class.
    TransactionAttribute txAttr = findTransactionAttribute(specificMethod);
    if (txAttr != null) {
        return txAttr;
    }

    // Second try is the transaction attribute on the target class.
    txAttr = findTransactionAttribute(specificMethod.getDeclaringClass());
    if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
        return txAttr;
    }

    if (specificMethod != method) {
        // Fallback is to look at the original method.
        txAttr = findTransactionAttribute(method);
        if (txAttr != null) {
            return txAttr;
        }
        // Last fallback is the class of the original method.
        txAttr = findTransactionAttribute(method.getDeclaringClass());
        if (txAttr != null && ClassUtils.isUserLevelMethod(method)) {
            return txAttr;
        }
    }

    return null;
}

判断类和方法是否存在,是否在接口上,则要获取真正的实现类等,考虑各种情况,真正的实现还是AnnotationTransactionAttributeSource自己处理。

protected TransactionAttribute findTransactionAttribute(Method method) {
	return determineTransactionAttribute(method);
}
protected TransactionAttribute determineTransactionAttribute(AnnotatedElement element) {
    for (TransactionAnnotationParser parser : this.annotationParsers) {
        TransactionAttribute attr = parser.parseTransactionAnnotation(element);
        if (attr != null) {
            return attr;
        }
    }
    return null;
}

遍历我们上面分析创建的所有解析器,进行解析,相当于委派模式。我们就查看SpringTransactionAnnotationParser解析就可以了。

public TransactionAttribute parseTransactionAnnotation(AnnotatedElement element) {
    AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(
            element, Transactional.class, false, false);
    if (attributes != null) {
        return parseTransactionAnnotation(attributes);
    }
    else {
        return null;
    }
}

根据传入的Method或者Class,按照注解@Transactional进行解析,再进行处理:

protected TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) {
    RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();

    Propagation propagation = attributes.getEnum("propagation");
    rbta.setPropagationBehavior(propagation.value());
    Isolation isolation = attributes.getEnum("isolation");
    rbta.setIsolationLevel(isolation.value());
    rbta.setTimeout(attributes.getNumber("timeout").intValue());
    rbta.setReadOnly(attributes.getBoolean("readOnly"));
    rbta.setQualifier(attributes.getString("value"));

    List<RollbackRuleAttribute> rollbackRules = new ArrayList<>();
    for (Class<?> rbRule : attributes.getClassArray("rollbackFor")) {
        rollbackRules.add(new RollbackRuleAttribute(rbRule));
    }
    for (String rbRule : attributes.getStringArray("rollbackForClassName")) {
        rollbackRules.add(new RollbackRuleAttribute(rbRule));
    }
    for (Class<?> rbRule : attributes.getClassArray("noRollbackFor")) {
        rollbackRules.add(new NoRollbackRuleAttribute(rbRule));
    }
    for (String rbRule : attributes.getStringArray("noRollbackForClassName")) {
        rollbackRules.add(new NoRollbackRuleAttribute(rbRule));
    }
    rbta.setRollbackRules(rollbackRules);

    return rbta;
}

终于看到了属性的,是否只读、事务隔离级别、事务边界、超时时间、回滚异常类等信息。封装返回,并进行缓存。

 

篇幅有限剩下的在 Spring源码-@EnableTransactionManagement方式事务实现分析(下)继续进行分析。

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值