Spring Boot @Trasactionl 失效, JDK,CGLIB动态代理

来自: https://www.cnblogs.com/sweetchildomine/p/6978037.html?utm_source=itdadao&utm_medium=referral

Cglib方式

1:添加注解

@EnableAspectJAutoProxy(exposeProxy = true)
//启动
@SpringBootApplication
public class WxoApplication {

2:修改yml配置

#开启Cglib代理
spring:
  aop:
    proxy-target-class: true

3:添加Aop依赖 默认mvc没有包括aop的

        <!-- MVC -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- AOP -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

4:调用和使用

class TransactionService implements ITransactionService {
  
  @Transaction
  public void mehtod1() {
    ((TransactionService) AopContext.currentProxy()).mehtod2();
  }
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public void mehtod2() {
    
}
}

  判断当前使用的代理模式

//logger.info("isAopProxy  :" + AopUtils.isAopProxy(AopContext.currentProxy()));
//logger.info("isCglibProxy:" + AopUtils.isCglibProxy(AopContext.currentProxy()));
//logger.info("isJdkProxy  :" + AopUtils.isJdkDynamicProxy(AopContext.currentProxy()));

getBean方式

  参考:http://www.cnblogs.com/eason-d/p/7840558.html

原文地址:https://www.cnblogs.com/eason-d/p/8329086.html