论坛首页 Java企业应用论坛

Spring事务处理时自我调用的解决方案及一些实现方式的风险

浏览 59740 次
该帖已经被评为精华帖
作者 正文
   发表时间:2012-04-16  
写的很不错。不过循环引用很难避免。我觉得最好给spring社区一个好的建议,或者下一个发布能改正这个问题会更好

0 请登录后投票
   发表时间:2012-04-16  
期待2.4,2.5问题剖析
0 请登录后投票
   发表时间:2012-04-16  
icanfly 写道
期待2.4,2.5问题剖析

正在赶制 只能利用晚上时间写!
0 请登录后投票
   发表时间:2012-04-16   最后修改:2012-04-16
javamonkey 写道
写的很不错。不过循环引用很难避免。我觉得最好给spring社区一个好的建议,或者下一个发布能改正这个问题会更好



spring允许的循环依赖(只考虑单例和原型Bean):
A----B
B----A
只有在A和B都不为原型是允许的,即如果A和B都是prototype则会报错(无法进行原型Bean的循环依赖)。
A(单例)---B(单例) 或 A(原型)---B(单例) 这是可以的,但 A(原型)---B(原型)或 A(原型)---B(单例Lazy)【且context.getBean("A")】时 这是不允许的。

一、A(原型)---B(原型) 或 A(原型)---B(单例Lazy)【且context.getBean("A")】 会报:
Error creating bean with name 'BServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sishuok.issue.AService com.sishuok.issue.impl.BServiceImpl.aService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'AServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.sishuok.issue.BService com.sishuok.issue.impl.AServiceImpl.bService; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'BServiceImpl': Requested bean is currently in creation: Is there an unresolvable circular reference?

二、A(原型)---B(单例) 和 A(单例)---B(单例)
这种方式 使用我的 【3.3 通过BeanPostProcessor 在目标对象中注入代理对象】 是没有问题的。

三、因此【3.4 改进版的InjectBeanSelfProcessor的解决方案】 可以作为最后的解决方案。
0 请登录后投票
   发表时间:2012-04-16  
学习了,写的很好!
0 请登录后投票
   发表时间:2012-04-16  
如果是同一个对象,调用不同的方法,只要该方法跟数据库打交道了,不就会根据配置的事务传递方式设置此方法所属的事务吗,当然如果不跟数据库打交道就没必要关心事务了,业务的事务,
0 请登录后投票
   发表时间:2012-04-16  
jyjava 写道
如果是同一个对象,调用不同的方法,只要该方法跟数据库打交道了,不就会根据配置的事务传递方式设置此方法所属的事务吗,当然如果不跟数据库打交道就没必要关心事务了,业务的事务,

只要目标对象的目标方法 内部调用时 才需要解决,其实更好的方式是避免内部调用
0 请登录后投票
   发表时间:2012-04-16  
分析得很好
0 请登录后投票
   发表时间:2012-04-16  
点进来之前就猜到是lz了
0 请登录后投票
   发表时间:2012-04-16  
xh176233756 写道
点进来之前就猜到是lz了

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics