使用 @Transactional 的 Java Spring 和 Groovy 应用程序 - 注入返回“代理”而不是所需的类型
我已将一些 groovy 文件添加到现有的 java + spring 项目中。
该项目正在运行(即通过了测试)。
我将“@Transactional”添加到其中一种常规方法中,并且测试失败了。
(为了添加@Transactional,我必须将类上的注释从@Service更改为@Controller,否则@Transactional将被忽略 - 恶心)。
我开始遇到异常
异常多种多样
- @Autowired 没有找到任何东西 - NoSuchBeanDefinitionException
- 添加 @Qualifier("loadTransactions") 给了我同样的结果
- @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
- @Autowired did not find anything - NoSuchBeanDefinitionException
- adding @Qualifier("loadTransactions") gave me the same
- @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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论