java Spring:注解驱动的事务管理器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4155991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring: Annotation-driven Transaction Manager
提问by John Manak
I'm setting up a new, JPA+Spring project. What is the difference (for me as a programmer) between:
我正在建立一个新的 JPA+Spring 项目。之间有什么区别(对我作为程序员而言):
<tx:annotation-driven transaction-manager="transactionManager" />
and
和
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
in my applicationContext.xml?
在我的 applicationContext.xml 中?
回答by Ralph
There is a huge difference between Proxies and byte code weaven aspects. Proxies can only intercept if the invocation comes from “outer space”, but not if the invocation comes from the object itself (this.transactionalMethod())
代理和字节码编织方面之间存在巨大差异。代理只能拦截来自“外层空间”的调用,但不能拦截来自对象本身的调用(this.transactionalMethod())
This means if you have a Class with two methods, T and B. Method T
has a transaction annotation, and method B
invokes T
by “this.T()
”, then the proxy is never invoked (for T
) so there is no transaction handling in this case!
这意味着,如果您有一个包含两个方法 T 和 B 的类。方法T
有一个事务注释,并且方法通过“B
调用,那么代理永远不会被调用(for ),因此在这种情况下没有事务处理!T
“this.T()
T
If you use AspectJ the transaction handling code is weaven in the byte code of T
, and it will be executed no matter if the invocation comes from the object itself or from an other object.
如果你使用 AspectJ,事务处理代码被编织在 的字节码中T
,无论调用是来自对象本身还是来自其他对象,它都会被执行。
回答by Bozho
The docssay:
文档说:
The default mode "proxy" processes annotated beans to be proxied using Spring's AOP framework (following proxy semantics, as discussed above, applying to method calls coming in through the proxy only). The alternative mode "aspectj" instead weaves the affected classes with Spring's AspectJ transaction aspect, modifying the target class byte code to apply to any kind of method call. AspectJ weaving requires spring-aspects.jar in the classpath as well as load-time weaving (or compile-time weaving) enabled. (See Section 7.8.4.5, “Spring configuration” for details on how to set up load-time weaving.)
默认模式“代理”处理使用 Spring 的 AOP 框架代理的带注释的 bean(遵循代理语义,如上所述,仅适用于通过代理传入的方法调用)。替代模式“aspectj”而是将受影响的类与 Spring 的 AspectJ 事务方面编织在一起,修改目标类字节码以应用于任何类型的方法调用。AspectJ 编织需要类路径中的 spring-aspects.jar 以及启用加载时编织(或编译时编织)。(有关如何设置加载时编织的详细信息,请参阅第 7.8.4.5 节,“Spring 配置”。)
It doesn't matter (from a developer's perspective) which mode will be used.
(从开发人员的角度来看)使用哪种模式并不重要。