环绕通知使用事务(@Around)

David 2022-11-28 17:03:32
Categories: Tags:

​​​​​​​

示例

@Pointcut("@annotation(com.exi.dao.Permissions)")

private void permissionCheck() {

}

@Around("execution(permissionCheck())")

public Object myAround(ProceedingJoinPoint pjp) {    

        try{

            PlatformTransactionManager.beginTransaction();//使用spring的事务管理器,开启事务       

            pjp.proceed(); //执行目标方法 

            //doSome()

            PlatformTransactionManager.commit();//业务方法正常执行,提交事务

        }catch(Exception e){

            PlatformTransactionManager.rollback();//业务方法正常执行,回滚事务    

    }

}