Spring当中 方法上的@Transactional 会覆盖类上的 @Transactional 原因

51 阅读1分钟
@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;
}