spring事務管理源碼分析(一)配置和事務加強代理的生成流程

在本篇文章中,將會介紹如何在spring中進行事務管理,以後對其內部原理進行分析。主要涉及java

  1. @EnableTransactionManagement註解爲咱們作了什麼?
  2. 爲何標註了@Transactional註解的方法就能夠具備事務的特性,保持了數據的ACID特性?spring究竟是如何具備這樣的偷樑換柱之術的?
  3. 中間涉獵到一些spring關於註解配置的解析邏輯分析,這一部分比較通用,並非spring事務管理模塊特有的功能。在日後分析spring其餘模塊代碼的時候能夠借鑑

如何在spring應用中使用事務

咱們能夠在配置類上標記註解@EnableTransactionManagement,這樣就能夠設置spring應用開啓事務管理。 以後咱們能夠在須要開啓事務的方法上標註@Transactional,spring將利用AOP框架,生成代理類,爲方法配置事務加強。下面看一個具體的例子spring

demo

配置類

@Configuration
@EnableTransactionManagement  // 咱們這一節的重點研究對象
public class MybatisConfig {

    // 各類其餘配置,如數據源配置、mybatis的SqlSessionFactory等
}
複製代碼

須要事務加強的接口類

// 接口類
public interface CountryService {
    int createCountry(Country country);

}

// 實現,咱們故意讓其拋出異常
public class CountryServiceImpl implements CountryService {

    // ... 注入countryMapper

    @Override
    @Transactional
    public int createCountry(Country country) {
        // 使用mybatis mapper來操做數據庫
        int result = countryMapper.insert(country);
        int i = 1 / 0; // 拋出RuntimeException,事務回滾
        return result;
    }
}
複製代碼

使用接口

public class ContextTest {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MybatisConfig.class);
        CountryService countryService = context.getBean(CountryService.class);
        Country country = new Country();
        country.setCountryName("中國");
        country.setCountryCode("CN");
        // 因爲咱們在countryService中,拋出了異常。所以這裏的數據將發生回滾,不會寫入到數據庫中
        countryService.createCountry(country);
    }
}
複製代碼

demo說明了什麼?

從以上的demo,咱們能夠看出來。利用spring的事務管理框架,咱們只須要三個步驟便可:數據庫

  1. 經過註解@EnableTransactionManagement,開啓spring的事務管理功能
  2. 在接口類的須要事務加強的方法上,標註@Transactional
  3. 在容器中使用加強後的代理類的事務方法,如countryService.createCountry(country)

spring是如何作到的呢?

區區三個步驟,就可讓咱們解耦數據訪問、事務管理這兩個功能模塊。神奇的背後,到底隱藏着什麼原理呢?mybatis

從@EnableTransactionManagement開始探險

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;

}
複製代碼

從註解的代碼中,能夠看出在其內部經過@Import註解導入了TransactionManagementConfigurationSelector類app

TransactionManagementConfigurationSelector
public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> {

	/** * {@inheritDoc} * @return {@link ProxyTransactionManagementConfiguration} or * {@code AspectJTransactionManagementConfiguration} for {@code PROXY} and * {@code ASPECTJ} values of {@link EnableTransactionManagement#mode()}, respectively */
	@Override
	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;
		}
	}
}
複製代碼

TransactionManagementConfigurationSelector實現了接口ImportSelector

spring加載配置流程

因爲整個解析配置的流程過於複雜,代碼量繁多。這裏就不一一列出具體代碼了。下面提供一個主流程的時序圖,有興趣的看官能夠跟着流程圖去瀏覽一下相關源碼。 框架

在spring解析配置的過程當中,將調用方法AutoProxyRegistrar#registerBeanDefinitions,最終向容器中註冊了自動代理生成器InfrastructureAdvisorAutoProxyCreator

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    boolean candidateFound = false;
    Set<String> annoTypes = importingClassMetadata.getAnnotationTypes();
    for (String annoType : annoTypes) {
        AnnotationAttributes candidate = AnnotationConfigUtils.attributesFor(importingClassMetadata, annoType);
        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) {
                // 該方法內部將註冊一個自動生成代理類(InfrastructureAdvisorAutoProxyCreator)
                AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
                if ((Boolean) proxyTargetClass) {
                    AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
                    return;
                }
            }
        }
    }
    if (!candidateFound) {
        String name = getClass().getSimpleName();
        logger.warn(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));
    }
}
複製代碼

並且,在解析配置的過程當中,將處理Import進來的配置類ProxyTransactionManagementConfiguration。 其內部存在三個用@Bean註解標註的方法以下,將向容器註冊其各自返回的bean。ide

@Configuration
public class ProxyTransactionManagementConfiguration extends AbstractTransactionManagementConfiguration {

    // 註冊一個切面
	@Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
		BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
		advisor.setTransactionAttributeSource(transactionAttributeSource());
		advisor.setAdvice(transactionInterceptor());
		advisor.setOrder(this.enableTx.<Integer>getNumber("order"));
		return advisor;
	}

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

    // 切面邏輯(Advice)
	@Bean
	@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
	public TransactionInterceptor transactionInterceptor() {
		TransactionInterceptor interceptor = new TransactionInterceptor();
		interceptor.setTransactionAttributeSource(transactionAttributeSource());
		if (this.txManager != null) {
			interceptor.setTransactionManager(this.txManager);
		}
		return interceptor;
	}

}

複製代碼
解析配置後的成果
  1. 註冊了自動代理生成器InfrastructureAdvisorAutoProxyCreator
  2. 註冊了ProxyTransactionManagementConfiguration,其內部會經過@Bean標註的方法,進而註冊BeanFactoryTransactionAttributeSourceAdvisor、AnnotationTransactionAttributeSource、TransactionInterceptor
spring是如何爲咱們進行事務加強的

spring經過AOP框架在容器啓動時,自動發現須要事務加強的類或方法(即標註了@Transactional的類或方法),爲這些方法嵌入事務切面(即BeanFactoryTransactionAttributeSourceAdvisor)生成代理類,以後咱們從容器獲取到的對應的bean就是進行事務加強後的代理類。大體的步驟包括:源碼分析

  1. InfrastructureAdvisorAutoProxyCreator做爲BeanPostProcessor,在容器啓動期間其postProcessAfterInitialization方法被調用,做爲建立事務加強代理對象的入口
  2. 以後從beanfactory中獲取全部Advisor實現類的實例,使用每個獲取到的Advisor中的Pointcut對當前正在建立的bean進行匹配,在這裏Advisor爲BeanFactoryTransactionAttributeSourceAdvisor、Pointcut爲TransactionAttributeSourcePointcut
  3. 匹配過程當中會調用TransactionAttributeSourcePointcut的matches(Method method, Class targetClass)方法來進行匹配判斷,判斷的工做須要藉助AnnotationTransactionAttributeSource#getTransactionAttribute(Method method, Class targetClass)來解析註解@Transactional
  4. 若是匹配成功,則證實須要生成事務加強代理。會返回BeanFactoryTransactionAttributeSourceAdvisor實例,做爲切面設置到ProxyFactory中,用於生成代理
  5. 經過ProxyFactory來生成事務加強代理

大體的流程圖以下所示 post

事務加強代理生成過程的源碼分析
InfrastructureAdvisorAutoProxyCreator(BeanPostProcessor)
public class InfrastructureAdvisorAutoProxyCreator extends AbstractAdvisorAutoProxyCreator {
	// postProcessAfterInitialization接口在其父類中實現
}
複製代碼
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if (bean != null) {
			Object cacheKey = getCacheKey(bean.getClass(), beanName);
			if (!this.earlyProxyReferences.contains(cacheKey)) {
				// 代理生成邏輯
				return wrapIfNecessary(bean, beanName, cacheKey);
			}
		}
		return bean;
	}

	protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
		if (beanName != null && 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.
		// 在這個方法內部將調用TransactionAttributeSourcePointcut#match方法進行匹配,若是匹配成功那麼會返回BeanFactoryTransactionAttributeSourceAdvisor實例
		// 這個方法完成了如下幾個工做
		// 1. 從beanfactory中獲取全部註冊到beanfactory中的Advisor,將Advisor進行實例化
		// 2. 調用Advisor中的Pointcut的matches方法,進行匹配。匹配成功則返回當前Advisor
		// 3. 在事務管理的框架中,匹配細節由TransactionAttributeSourcePointcut#matches方法負責,其內部會調用AnnotationTransactionAttributeSource#getTransactionAttribute方法解析@Transactional註解
		// 4. 對於局部事務來講,解析@Transactional的解析將委託給SpringTransactionAnnotationParser
		Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
		if (specificInterceptors != DO_NOT_PROXY) {
			this.advisedBeans.put(cacheKey, Boolean.TRUE);
			// 在這個方法內部將使用ProxyFactory來生成代理
			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;
	}

}
複製代碼
BeanFactoryTransactionAttributeSourceAdvisor(Advisor)

這裏先看一下BeanFactoryTransactionAttributeSourceAdvisor的類圖,大概瞭解下這個類是在整個事務管理體系中是屬於什麼角色。 ui

public class BeanFactoryTransactionAttributeSourceAdvisor extends AbstractBeanFactoryPointcutAdvisor {

	private TransactionAttributeSource transactionAttributeSource;

    // 切面內的Pointcut,用於在生成代理的過程當中進行匹配方法
	private final TransactionAttributeSourcePointcut pointcut = new TransactionAttributeSourcePointcut() {
		@Override
		protected TransactionAttributeSource getTransactionAttributeSource() {
			return transactionAttributeSource;
		}
	};


	/** * Set the transaction attribute source which is used to find transaction * attributes. This should usually be identical to the source reference * set on the transaction interceptor itself. * @see TransactionInterceptor#setTransactionAttributeSource */
	public void setTransactionAttributeSource(TransactionAttributeSource transactionAttributeSource) {
		this.transactionAttributeSource = transactionAttributeSource;
	}

	/** * Set the {@link ClassFilter} to use for this pointcut. * Default is {@link ClassFilter#TRUE}. */
	public void setClassFilter(ClassFilter classFilter) {
		this.pointcut.setClassFilter(classFilter);
	}

	@Override
	public Pointcut getPointcut() {
		return this.pointcut;
	}

}
複製代碼

能夠看出,這個類間接實現了接口PointcutAdvisor,這是一個切面類(即組合了Pointcut、Advice)。其內部定義的Pointcut爲抽象類TransactionAttributeSourcePointcut的匿名實現類。關於AOP的這些概念,能夠參考:spring-AOP原理分析一spring-AOP原理分析二,這裏再也不贅述。

TransactionAttributeSourcePointcut(Pointcut)

這個類間接實現了兩個接口:Pointcut、MethodMatcher。在事務管理中做爲AOP的pointcut、methodMatcher兩個角色。用於匹配方法是否須要進行事務加強

abstract class TransactionAttributeSourcePointcut extends StaticMethodMatcherPointcut implements Serializable {

    // 經過AnnotationTransactionAttributeSource來獲取
	@Override
	public boolean matches(Method method, Class<?> targetClass) {
		if (targetClass != null && TransactionalProxy.class.isAssignableFrom(targetClass)) {
			return false;
		}
		// 獲取到的tas爲AnnotationTransactionAttributeSource實例,
		// 在ProxyTransactionManagementConfiguration中註冊而來
		TransactionAttributeSource tas = getTransactionAttributeSource();
		return (tas == null || tas.getTransactionAttribute(method, targetClass) != null);
	}

	@Override
	public boolean equals(Object other) {
		if (this == other) {
			return true;
		}
		if (!(other instanceof TransactionAttributeSourcePointcut)) {
			return false;
		}
		TransactionAttributeSourcePointcut otherPc = (TransactionAttributeSourcePointcut) other;
		return ObjectUtils.nullSafeEquals(getTransactionAttributeSource(), otherPc.getTransactionAttributeSource());
	}

	@Override
	public int hashCode() {
		return TransactionAttributeSourcePointcut.class.hashCode();
	}

	@Override
	public String toString() {
		return getClass().getName() + ": " + getTransactionAttributeSource();
	}


	/** * Obtain the underlying TransactionAttributeSource (may be {@code null}). * To be implemented by subclasses. */
	protected abstract TransactionAttributeSource getTransactionAttributeSource();

}
複製代碼
AnnotationTransactionAttributeSource

在TransactionAttributeSourcePointcut#matches(Method method, Class targetClass)方法中,將調用AnnotationTransactionAttributeSource#getTransactionAttribute(Method method, Class targetClass)方法,用於獲取TransactionAttribute,即配置到@Transactional的屬性值。實際的獲取動做代理給了父類AbstractFallbackTransactionAttributeSource

public class AnnotationTransactionAttributeSource extends AbstractFallbackTransactionAttributeSource implements Serializable {

        // ... 省略code

}

public abstract class AbstractFallbackTransactionAttributeSource implements TransactionAttributeSource {

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

		// First, see if we have a cached value.
		Object cacheKey = getCacheKey(method, targetClass);
		Object 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 (TransactionAttribute) 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.isDebugEnabled()) {
					logger.debug("Adding transactional method '" + methodIdentification + "' with attribute: " + txAttr);
				}
				this.attributeCache.put(cacheKey, txAttr);
			}
			return txAttr;
		}
	}

    // 解析@Transaction註解的屬性值
    protected TransactionAttribute computeTransactionAttribute(Method method, Class<?> targetClass) {
		// Don't allow no-public methods as required.
		// 1. 只有public方法能夠切入事務管理
		if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) {
			return null;
		}

		// Ignore CGLIB subclasses - introspect the actual user class.
		Class<?> userClass = ClassUtils.getUserClass(targetClass);
		// 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 = ClassUtils.getMostSpecificMethod(method, userClass);
		// If we are dealing with method with generic parameters, find the original method.
		specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);

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

	@Override
	protected TransactionAttribute findTransactionAttribute(Method method) {
		return determineTransactionAttribute(method);
	}

	protected TransactionAttribute determineTransactionAttribute(AnnotatedElement ae) {
		if (ae.getAnnotations().length > 0) {
			// TransactionAnnotationParser的實現類有Ejb3TransactionAnnotationParser、JtaTransactionAnnotationParser、SpringTransactionAnnotationParser
			// 對於局部失誤,咱們使用SpringTransactionAnnotationParser來進行解析
			for (TransactionAnnotationParser annotationParser : this.annotationParsers) {
				TransactionAttribute attr = annotationParser.parseTransactionAnnotation(ae);
				if (attr != null) {
					return attr;
				}
			}
		}
		return null;
	}

}

public class SpringTransactionAnnotationParser implements TransactionAnnotationParser, Serializable {

	@Override
	public TransactionAttribute parseTransactionAnnotation(AnnotatedElement ae) {
		AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ae, Transactional.class);
		if (attributes != null) {
			return parseTransactionAnnotation(attributes);
		}
		else {
			return null;
		}
	}

	public TransactionAttribute parseTransactionAnnotation(Transactional ann) {
		return parseTransactionAnnotation(AnnotationUtils.getAnnotationAttributes(ann, false, false));
	}

	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"));
		ArrayList<RollbackRuleAttribute> rollBackRules = new ArrayList<RollbackRuleAttribute>();
		Class<?>[] rbf = attributes.getClassArray("rollbackFor");
		for (Class<?> rbRule : rbf) {
			RollbackRuleAttribute rule = new RollbackRuleAttribute(rbRule);
			rollBackRules.add(rule);
		}
		String[] rbfc = attributes.getStringArray("rollbackForClassName");
		for (String rbRule : rbfc) {
			RollbackRuleAttribute rule = new RollbackRuleAttribute(rbRule);
			rollBackRules.add(rule);
		}
		Class<?>[] nrbf = attributes.getClassArray("noRollbackFor");
		for (Class<?> rbRule : nrbf) {
			NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(rbRule);
			rollBackRules.add(rule);
		}
		String[] nrbfc = attributes.getStringArray("noRollbackForClassName");
		for (String rbRule : nrbfc) {
			NoRollbackRuleAttribute rule = new NoRollbackRuleAttribute(rbRule);
			rollBackRules.add(rule);
		}
		rbta.getRollbackRules().addAll(rollBackRules);
		return rbta;
	}

	@Override
	public boolean equals(Object other) {
		return (this == other || other instanceof SpringTransactionAnnotationParser);
	}

	@Override
	public int hashCode() {
		return SpringTransactionAnnotationParser.class.hashCode();
	}

}
複製代碼

總結

以上咱們從一個demo入手,瞭解瞭如何使用spring來管理事務;以後咱們從配置的註解@EnableTransactionManagement切入到spring事務框架的內部原理。期間涉及了幾個主要的類:

  1. AutoProxyRegistrar其主要職責是註冊InfrastructureAdvisorAutoProxyCreator 1.1 InfrastructureAdvisorAutoProxyCreator,負責在容器啓動期間利用配置信息生成事務加強的代理類對象
  2. ProxyTransactionManagementConfiguration其主要職責是註冊AnnotationTransactionAttributeSource、TransactionInterceptor、BeanFactoryTransactionAttributeSourceAdvisor 2.1 AnnotationTransactionAttributeSource負責解析@Transactional註解 2.2 BeanFactoryTransactionAttributeSourceAdvisor做爲切面,組合了Pointcut和Advice提供了事務管理的功能
  3. TransactionAttributeSourcePointcut做爲Pointcut,負責匹配方法
  4. TransactionInterceptor做爲Advice,事務管理的邏輯都在這個類中進行實現。

懸念

鑑於篇幅過長了,O(≧口≦)O。。。下一節咱們再對事務管理的邏輯進行剖析,即對TransactionInterceptor進行分析

相關文章
相關標籤/搜索