使用 @Transactional 的 Java Spring 和 Groovy 应用程序 - 注入返回“代理”而不是所需的类型

发布于 2024-12-22 04:34:01 字数 865 浏览 5 评论 0原文

我已将一些 groovy 文件添加到现有的 java + spring 项目中。
该项目正在运行(即通过了测试)。

我将“@Transactional”添加到其中一种常规方法中,并且测试失败了。
(为了添加@Transactional,我必须将类上的注释从@Service更改为@Controller,否则@Transactional将被忽略 - 恶心)。

我开始遇到异常
异常多种多样

  1. @Autowired 没有找到任何东西 - NoSuchBeanDefinitionException
  2. 添加 @Qualifier("loadTransactions") 给了我同样的结果
  3. @Resource 给出了 BeanNotOfRequiredTypeException:名为“loadTransactions”的 Bean 必须属于 [net.idt.workflow.LoadTransactions] 类型,但实际上属于 [$Proxy33] 类型
    (我发现这是一个比我总是使用的 @Autowired 更好的错误消息:-( )

我将类型更改为“Object”并且 @Resource 注入起作用了(我对 @Autowired 有点恼火 - 所以我没有费心尝试使用 Object :-/ )

然后我使用了以下常规代码:
  loadTransactions.metaClass.object.process();
我觉得“很臭”,但是有效。

我的问题是:如何获得我期望的漂亮代码,即
  loadTransactions.process();
我不必知道 @Transactional 正在使用某些代理

感谢您的帮助
沙洛姆·戴奇

I have added some groovy files to an existing java + spring project.
The project was working (i.e. past the tests).


I added "@Transactional" to one of the groovy methods and the tests broke.

(In order to add @Transactional, I had to change the Annotation on the class from @Service to @Controller, otherwise the @Transactional was ignored - yuck).


I started getting exceptions
The exceptions varied

  1. @Autowired did not find anything - NoSuchBeanDefinitionException
  2. adding @Qualifier("loadTransactions") gave me the same
  3. @Resource gave BeanNotOfRequiredTypeException: Bean named 'loadTransactions' must be of type [net.idt.workflow.LoadTransactions], but was actually of type [$Proxy33]

    (I find this a better error message then the @Autowired that I always use :-( )

I changed the type to "Object" and the @Resource injection worked (I am a bit annoyed with @Autowired - so I did not bother trying that with Object :-/ )

I then used the following groovy code:
  loadTransactions.metaClass.object.process();
which I feel "stinks", but WORKS.


My question is: how do I get the nice code that I expected i.e.

  loadTransactions.process();
I should not have to know that the @Transactional is using some proxy

Thanks for your help
Shalom Deitch

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文