tx:注解驱动在scala中破坏@Autowired
我正在使用 spring+scala 2.8。我有一个@Transactional bean(用@Service标记),它在spring中通过<启用> tx:annotation-driven / >,当我启动 tomcat 时,使用此服务 bean 获取 @Autowired 的控制器找不到自动装配候选者。当 @Service 对象明显被实例化时(我可以通过 log4j 调试消息看到这一点),我一直在试图找出为什么它找不到候选对象。我最终决定通过注释掉 < 来简化。 tx:annotation-driven / >,瞧!网络应用程序启动正常。
有人可以提供这不起作用的技术原因吗?我即将尝试 @Qualifier 看看是否可以“解决”问题。
I'm using spring+scala 2.8. I have a @Transactional bean (marked with @Service), that is enabled in spring via < tx:annotation-driven / >, and when I fire up tomcat the controller that gets @Autowired with this service bean can't find an autowire candidate. I was beating myself up trying to figure out why it couldn't find a candidate object, when the @Service object was clearly getting instantiated (I could see this via log4j debug messages). I eventually decided to simplify by commenting out the < tx:annotation-driven / >, and voila! The webapp starts fine.
Can someone provide a technical reason why this doesn't work? I'm about to try @Qualifier to see if that "fixes" the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您将
添加到配置中时,它会导致代理类发生各种包装(请参阅 Spring 事务文档)。这可能与 @Transactional 注释和 Scala 配合得不好。请参阅在 Scala 中使用 Spring @Transactional
When you add
<tx:annotation-driven/>
to your configuration, it causes various wrapping to happen with proxy classes (see the Spring documentation on transactions). This probably isn't playing well with the @Transactional annotation and Scala.See Use Spring @Transactional in Scala
所以我弄清楚了我的问题。实际上,它可能已经被 java 和 scala 所“破坏”了。我的服务类仅包含一个实现类,没有接口。一旦 @Transactional 代理,类 spring 就无法找到自动装配候选者。只需添加一个接口(或 scala 中的特征)即可解决该问题。
So I figured out my issue. It actually may have been "broken" with java as well as scala. My service class consisted only of an implementation class and no interface. Once @Transactional proxied the class spring was unable to find an autowire candidate. Simply adding an interface (or trait in scala) fixed the issue.