[记录]内部方法之间调用AOP无效解决方法

起因:

测试的时候发现@Transactional(rollbackFor = Exception.class)即便抛出了异常也不回滚

原因:

@Transactional等注解均是基于AOP代理,而方法自身内部调用不会经过AOP。

this.insert(xxx);

就是内部调用

解决:

推荐第二种,第一种修改了入口类注解,不确定会不会引发其他问题

1.

springboot入口类增加注解

@EnableAspectJAutoProxy(exposeProxy = true)

调用处获取自身代理(也可在内部注入自身)调用

((XXX(AopContext.currentProxy())).insert(xxx);

2.

    @Autowired
    private ApplicationContext applicationContext;

	GeofenceService geofenceService = applicationContext.getBean(GeofenceService.class);

你可能感兴趣的:(Java)