service 的自调用中this与AOP代理的区别

使用ssm过程中,遇到了service自调的问题,于是研究了this与AOP代理两种方法。测试类:public interface IService { void a(); void b();}@Service("service")public class ServiceImpl implements IService{ @Override @Transactio...
摘要由CSDN通过智能技术生成

使用ssm过程中,遇到了service自调的问题,于是研究了this与AOP代理两种方法。

测试类:

public interface IService {
	void a();
	void b();
}




@Service("service")
public class ServiceImpl implements IService{

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void a() {

     // this.b();

        ((IService) AopContext.currentProxy()).b();
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void b() {  }		
}

this.b()的方式:

如果 a 方法使用 this.b( ) 的方式调用方法,b 事务是不会增强的,Spring 文档中说过,只有外部调动才会增强, this 指的是当前对象,而不是 proxy 对象,所以事务不会增强。这时候 b 方法上的                                                                         @Transactional(propagation = Propagation.REQUIRES_NEW) 不起作用,也就是说这时候 b 方法与 a 方法的事务定义是一样的,这一点可以从日志中看出:

Returning cached instance of singleton bean 'service'
Adding transactional method 'ServiceImpl.a' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Returning cached instance of singleton bean 'transactionManager'

//创建新事务(a事务)
Creating new transaction with name [com.llangzh.services.impls.ServiceImpl.a]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''


incremented pending_acquires: 1
incremented pending_acquires: 2
incremented pending_acquires: 3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值