Spring @Transactional既作为动态Jdk代理又作为aspectj方面应用 [英] Spring @Transactional is applied both as a dynamic Jdk proxy and an aspectj aspect

查看:230
本文介绍了Spring @Transactional既作为动态Jdk代理又作为aspectj方面应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过@Transactional注释向现有Java项目添加Spring声明性事务。

I am in the process of adding Spring declarative transactions via the @Transactional annotation to an existing Java project.

当我遇到问题时(与此问题无关) ),我打开了完整的调试日志。奇怪的是,我注意到以下内容:

When I ran into a problem (unrelated to this question), I turned on full debug logging. Curiously, I noticed the following:


17:47:27,834 DEBUG HibernateTransactionManager:437 - Found thread-bound Session [org.hibernate.impl.SessionImpl@10ed8a8e] for Hibernate transaction
17:47:27,845 DEBUG HibernateTransactionManager:470 - Participating in existing transaction
17:47:27,865 DEBUG AnnotationTransactionAttributeSource:106 - Adding transactional method 'updateUserProfile' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
17:47:27,875 DEBUG AnnotationTransactionAspect:321 - Skipping transactional joinpoint [se.myservice.UserService.updateUserProfile] because no transaction manager has been configured

经过一些调试后,我发现前三个日志条目,即它发现一个线程绑定会话并使用该事务,是由我的UserService类上的JdkDynamicAopProxy生成的。

After some debugging, I found out that the first three log entries, where it says it found a thread-bound session and is using that transaction, is produced by a JdkDynamicAopProxy on my UserService class.

最后一条日志消息看起来很惊人。在方法执行之前在连接点调用它。查看AnnotationTransactionAspect的源时,如果未设置事务管理器,则会生成此消息。在这种情况下,因为Spring从不在这方面执行任何依赖注入。

The last log message looks alarming though. It is invoked at a joinpoint before the method execution. When looking at the source for AnnotationTransactionAspect, it produces this message if no transaction manager has been set. In this case, because Spring never performs any dependency injection on this aspect.

在我看来,两种不同的样式事务都适用于:动态代理,和方面。我唯一与交易相关的配置是:

It looks to me like two different "styles" of transactions are both applied: the dynamic proxy, AND the aspect. The only transaction-related configuration I have is:

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

我们在项目中使用了AspectJ,但是在我的aop.xml中没有注册AnnotationTransactionAspect方面。我们正在使用Spring 3.0.2.RELEASE。

We are using AspectJ in the project, but there is no AnnotationTransactionAspect aspect registered in my aop.xml. We are using Spring 3.0.2.RELEASE.

我应该对此感到震惊吗? Spring会为我注册这个方面吗?使用AspectJ时,我不应该使用注释驱动的吗?

Should I be alarmed by this? Does Spring register this aspect for me? Should I not use annotation-driven when using AspectJ?

推荐答案

奇怪,听起来你有这样的配置:

Strange, it sounds like you have this configuration:

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

(使用AspectJ的交易支持,而不是JDK代理)

(Transaction support using AspectJ, not JDK proxies)

由于您的配置没有mode属性,因此默认应该启动(代理模式)。但是AnnotationTransactionAspect是aspectj模式使用的确切方面。

Since your config doesn't have a mode attribute, the default should kick in (proxy mode). But AnnotationTransactionAspect is the exact aspect used by the aspectj mode.

这篇关于Spring @Transactional既作为动态Jdk代理又作为aspectj方面应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆