1. 程式人生 > >spring事務管理原始碼分析(一)配置和事務增強代理的生成流程

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

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

  1. @EnableTransactionManagement註解為我們做了什麼?
  2. 為什麼標註了@Transactional註解的方法就可以具有事務的特性,保持了資料的ACID特性?spring到底是如何具有這樣的偷樑換柱之術的?
  3. 中間涉獵到一些spring關於註解配置的解析邏輯分析,這一部分比較通用,並不是spring事務管理模組特有的功能。在往後分析spring其他模組程式碼的時候可以借鑑

如何在spring應用中使用事務

我們可以在配置類上標記註解@EnableTransactionManagement,這樣就可以設定spring應用開啟事務管理。 之後我們可以在需要開啟事務的方法上標註@Transactional,spring將利用AOP框架,生成代理類,為方法配置事務增強。下面看一個具體的例子

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是如何做到的呢?

區區三個步驟,就可以讓我們解耦資料訪問、事務管理這兩個功能模組。神奇的背後,到底隱藏著什麼原理呢?

從@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類

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。

@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來生成事務增強代理

大致的流程圖如下所示

事務增強代理生成過程的原始碼分析
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的類圖,大概瞭解下這個類是在整個事務管理體系中是屬於什麼角色。

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進行分析