自动接线的Spring bean不是代理 [英] Autowired spring bean is not a proxy

查看:133
本文介绍了自动接线的Spring bean不是代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个非常小的应用程序,该应用程序连接到MySQL数据库.

I'm working on a very small application connecting to a MySQL database.

我正在尝试创建表记录,但是没有正在进行的交易".

I'm trying to create table record but getting 'no transaction in progress'.

我准备好了所有合适的东西:

I have all the right stuff in place:

  • 服务接口MyService及其实现MyServiceImpl
  • 我已经用@Service注释了服务隐含
  • 在控制器中,我为@Autowired MyService字段使用了接口名称
  • 我具有正确的事务配置,因为它最初是由roo生成的
  • 有一个MyServiceImpl实现的公共方法MyService.create(...)

但是

当我进行远程调试并检查控制器的myService字段时,我看到的类似于 com.some.package.services.MyService@12345(而不是$ Proxy73之类的东西),这对我来说是不对的,因为应该自动连接的是代理而不是目标Bean(我认为这就是目标).如果我是正确的,那么就没有事务是有道理的,因为只有在调用代理上用@Transactional注释的公共方法时,注释才会启动.

When I remote debug and inspect the controller's myService field what I see is something like com.some.package.services.MyService@12345 (and NOT something like $Proxy73) which to me is not right, because what should be autowired is the proxy not he target bean (which is what I think this is). If I'm correct then it makes sense that there is no transaction as the annotation would only kick in when invoking a public method annotated with @Transactional on a proxy.

请告诉我为什么在这种设置中春季注入了目标豆.

Please tell me why is spring injecting the target bean in this setup.

谢谢

推荐答案

如果启用了AspectJ的事务管理(<tx:annotation-driven mode="aspectj" .../>),则在同一类中发生事务就地的应用在构建过程中(编译时编织)或在启动过程中(加载时编织).

If you have AspectJ-enabled transaction management (<tx:annotation-driven mode="aspectj" .../>) application of transactions happens in-place in the same class, either during build (compile-time weaving) or on startup (load-time weaving).

不会创建新的类(例如,使用),并且没有代理(例如,在Spring中基于普通的基于接口的AOP).相反,MyServiceImpl的字节码被直接修改,甚至没有引起您的注意.不幸的是,查看AOP的唯一方法是反编译您的类.如果使用javap -c MyServiceImpl,您会发现大量对Spring事务层的引用.

No new classes are created (like when using cglib) and no proxies (like with ordinary interface-based AOP in Spring). Instead bytecode of MyServiceImpl was modified directly without you even noticing. Unfortunately the only way to see AOP is to decompile your classes. If you use javap -c MyServiceImpl you'll find plenty of references to Spring transaction layer.

这篇关于自动接线的Spring bean不是代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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