接上節內容,Spring事務執行原理經過建立一個BeanFactoryTransactionAttributeSourceAdvisor,並把TransactionInterceptor注入進去,而TransactionInterceptor實現了Advice接口。而Spring Aop在Spring中會把Advisor中的Advice轉換成攔截器鏈,而後調用。ui
執行流程this
具體分析code
final TransactionAttribute txAttr = getTransactionAttributeSource().getTransactionAttribute(method, targetClass);
protected TransactionAttribute computeTransactionAttribute(Method method, Class<?> targetClass) { // Don't allow no-public methods as required. //1. allowPublicMethodsOnly()返回true,只能是公共方法 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表明實現類的方法 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; }
getTransactionAttributeSource()得到的對象是在ProxyTransactionManagementConfiguration建立bean時注入的AnnotationTransactionAttributeSource對象。 AnnotationTransactionAttributeSource中getTransactionAttributeSource方法主要邏輯交給了computeTransactionAttribute方法,因此咱們直接看computeTransactionAttribute代碼實現。orm
computeTransactionAttribute方法執行的邏輯是:對象
因此若是一個方法上用了@Transactional,類上和接口上也用了,以方法上的爲主,其次纔是類,最後纔到接口。接口
protected PlatformTransactionManager determineTransactionManager(TransactionAttribute txAttr) { // Do not attempt to lookup tx manager if no tx attributes are set if (txAttr == null || this.beanFactory == null) { return getTransactionManager(); } String qualifier = txAttr.getQualifier(); if (StringUtils.hasText(qualifier)) { return determineQualifiedTransactionManager(qualifier); } else if (StringUtils.hasText(this.transactionManagerBeanName)) { return determineQualifiedTransactionManager(this.transactionManagerBeanName); } else { //經常使用的會走到這裏 PlatformTransactionManager defaultTransactionManager = getTransactionManager(); if (defaultTransactionManager == null) { defaultTransactionManager = this.transactionManagerCache.get(DEFAULT_TRANSACTION_MANAGER_KEY); if (defaultTransactionManager == null) { //從beanFactory獲取PlatformTransactionManager類型的bean defaultTransactionManager = this.beanFactory.getBean(PlatformTransactionManager.class); this.transactionManagerCache.putIfAbsent( DEFAULT_TRANSACTION_MANAGER_KEY, defaultTransactionManager); } } return defaultTransactionManager; } }
@Bean public PlatformTransactionManager txManager() { return new DataSourceTransactionManager(dataSource()); }