简体   繁体   中英

Spring: Annotation-driven Transaction Manager

I'm setting up a new, JPA+Spring project. What is the difference (for me as a programmer) between:

<tx:annotation-driven transaction-manager="transactionManager" />

and

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

in my applicationContext.xml?

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 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!

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.

The docs say:

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.)

It doesn't matter (from a developer's perspective) which mode will be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM