我正在学习 AOP
并且开始知道这对于关注点分离很有用,例如,logging
, transaction management
, 安全
等
到目前为止很高兴了解 AOP
。
现在,我在 Spring 框架中阅读了 Spring 事务管理,我们可以使用注释 @Transactional
。
正是这一点让我感到困惑,为什么我们应该使用我们必须在其中创建 Aspects
的 AOP
而不是使用 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/