gpt4 book ai didi

java - Spring @Transactional 作为动态 Jdk 代理和 aspectj 切面应用

转载 作者:搜寻专家 更新时间:2023-10-31 08:24:38 24 4
gpt4 key购买 nike

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

当我遇到问题(与这个问题无关)时,我打开了完整的调试日志记录。奇怪的是,我注意到以下几点:

17:47:27,834 DEBUG HibernateTransactionManager:437 - Found thread-bound Session [org.hibernate.impl.SessionImpl@10ed8a8e] for Hibernate transaction17:47:27,845 DEBUG HibernateTransactionManager:470 - Participating in existing transaction17: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

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.

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。

我应该对此感到震惊吗? Spring 会帮我注册这方面吗?使用 AspectJ 时是否应该使用 annotation-driven

最佳答案

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

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

(使用 AspectJ 而非 JDK 代理的事务支持)

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

关于java - Spring @Transactional 作为动态 Jdk 代理和 aspectj 切面应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3573188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com