spring - 为什么在Spring中使用AOP进行事务管理?

标签 spring transactions aop cross-cutting-concerns

我正在学习 AOP 并且开始知道这对于关注点分离很有用,例如,logging, transaction management, 安全

到目前为止很高兴了解 AOP

现在,我在 Spring 框架中阅读了 Spring 事务管理,我们可以使用注释 @Transactional

正是这一点让我感到困惑,为什么我们应该使用我们必须在其中创建 AspectsAOP 而不是使用 Spring 提供的注释。

例如:

@Transactional
public void dataAccessTrans() {

}

当Spring已经有了事务相关的功能,那我们为什么还要用AOP来做事务管理呢?

如果我们使用AOP,那么我们不必创建Aspect 和创建advice 来作用于方法; 这不是让我们去做手工工作吗,而不是通过 spring 框架本身的注解来处理它。

任何人都可以帮助我理解这一点,我无法清楚地理解。

最佳答案

Spring依赖于 AOP 来实现声明式事务。

The Spring Framework’s declarative transaction management is made possible with Spring aspect-oriented programming (AOP), although, as the transactional aspects code comes with the Spring Framework distribution and may be used in a boilerplate fashion, AOP concepts do not generally have to be understood to make effective use of this code.

因此,当您使用 @Transactional 注释时:

@Transactional
public void dataAccessTrans() {
  ...
}

你间接使用了AOP。
因此在大多数情况下,您永远不需要声明任何自定义方面来处理事务管理。

关于spring - 为什么在Spring中使用AOP进行事务管理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48476530/

相关文章:

spring - 如何测试 Spring 集成

java - JTA 事务中使用了哪个超时

c# - TransactionScope 在处置前已中止交易

c# - 命令的事务属性尚未初始化

java - AspectJ 处理多个匹配建议

java - 有什么方法可以将所有 IOException 包装到 RuntimeException 中吗?

java - 通过删除不需要的依赖项或替换启动器来减小 Java Spring 应用程序的大小

java - 确保 Hibernate session 中的唯一序列号

SpringAOP 生成的动态子类缺少注解

c# - 使用代理在 C# 中拦截方法调用