几年前记得整理过,@Transacitonal注解的方法被另外一个方法调用的时候,事务是不生效的。

如果大量代码已经这么写了,这个时候抽取出去不现实,怎么办呢?

答案就是在<aop:aspectj-autoproxy />中设置expose-proxy属性为true暴露代理。如下:

<aop:aspectj-autoproxy expose-proxy=“true”> ,然后使用AopContext.currentProxy()获取当前代理,将this.b()改为((UserService)AopContext.currentProxy()).b(),这样就生效了。完整的例子如下:

public interface UserService{
public void a();
public void a();
} public class UserServiceImpl implements UserService{
@Transactional(propagation = Propagation.REQUIRED)
public void a(){
this.b();
}
@Transactional(propagation = Propagation.REQUIRED_NEW)
public void b(){
System.out.println("b has been called");
}
}

对应的spring boot注解为@EnableAspectJAutoProxy(exposeProxy=true)

@Transacitonal注解不生效之spring中expose-proxy的作用与原理的更多相关文章

  1. Spring 中的反射与反射的原理

    作者:DeppWang.原文地址 在造轮子:实现一个简易的 Spring IoC 容器一文中提到 Spring 在创建 Bean 实例和依赖注入时使用了反射,本文来具体分析一下 Spring 中的反射 ...

  2. Spring中<context:annotation-config/>的作用

    spring中<context:annotation-config/>配置的作用,现记录如下: <context:annotation-config/>的作用是向Spring容 ...

  3. Spring中的beanPostProcess的作用

    BeanPostProcessor是Spring框架中非常重要的bean之一.贯穿在Spring容器中bean的初始化的整个过程. Spring中的beanpostProcess体系结构如下: 可以看 ...

  4. spring 中的断言的作用

    org.springframework.util.AssertAssert翻译为中文为"断言".用过JUNIT的应该都知道这个概念了.就是断定某一个实际的值就为自己预期想得到的,如 ...

  5. Spring中Mybatis的花样配置 及 原理

    摘自: https://www.jianshu.com/p/fc23c94fc439

  6. cache在spring中使用

    一:参考文章 (1)http://haohaoxuexi.iteye.com/blog/2123030  Spring使用Cache,这篇文章讲的比较详细. 注:本文是对参考文章和实际使用中经验的总结 ...

  7. Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

  8. spring中注解式事务不生效的问题

    常用的解决方法可以百度,我针对我的问题描述一下 Mysql中InnoDB引擎才支持事务, MyISAM不支持事务. 当你尝试了各种方法解决spring中注解式事务不生效时, 一定要查看一下数据库中表的 ...

  9. Spring中@Autowired注解与自动装配

    1 使用配置文件的方法来完成自动装配我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. ...

随机推荐

  1. LeetCode - 868. Binary Gap

    Given a positive integer N, find and return the longest distance between two consecutive 1's in the ...

  2. link元素 rel src href属性

    The SRC and HREF attributes are used to include some external entities like an image, a CSS file, a ...

  3. PXC 57 二进制安装

    1.准备阶段 1.1 在三个节点上分别创建:用户组 用户组 目录 --用户组 用户组 #/usr/sbin/groupadd mysql #/usr/sbin/useradd -g mysql mys ...

  4. 第七天 py

  5. D - Bridge

    n people wish to cross a bridge at night. A group of at most two people may cross at any time, and e ...

  6. vue2.0 源码解读(一)

    又看完一遍中文社区的教程接下来开始做vue2.0的源码解读了! 注:解读源码时一定要配合vue2.0的生命周期和API文档一起看 vue2.0的生命周期分为4主要个过程 create. 创建---实例 ...

  7. vue computed的执行问题

    1.在new Vue()的时候,vue\src\core\instance\index.js里面的_init()初始化各个功能 function Vue (options) { if (process ...

  8. hash 位运算 练习

    hash  位运算 [以下代码仅做位运算的练习,算法本身不合理  php转译python] 从头到尾彻底解析Hash表算法_知识库_博客园 https://kb.cnblogs.com/page/18 ...

  9. 浅谈Java对象的equals方法

    相等与同一: 如果两个对象具有相同的类型以及相同的属性值,则称这两个对象相等. 如果两个引用对象指的是同一个对象,则称这两个变量同一. ==是一个比较运算符,基本数据类型比较的是值,引用数据类型比较的 ...

  10. HTTP协议工作原理

    HTTP简介        超文本传输协议(HTTP:Hypertext Transport Protocol)是万维网应用层的协议,它通过两个程序实现:一个是客户端程序(各种浏览器),另一个是服务器 ...