springboot aop无效的情况

项目结构
在这里插入图片描述

package com.example.demo.inter;

public interface CustomerService {
     void doSomething1();
     void doSomething2();
}
package com.example.demo.inter;

import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Override
    public void doSomething1() {
        System.out.println("CustomerServiceImpl.doSomething1()");
        doSomething2();
        ((CustomerService) AopContext.currentProxy()).doSomething2();
    }

    @Override
    public void doSomething2() {
        System.out.println("CustomerServiceImpl.doSomething2()");
    }

}
package com.example.demo;


import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class CustomerServiceInterceptor {

    @Before("execution(* com.example.demo.inter..*.*(..))")
    public void doBefore() {
        System.out.println("do some important things before...");
    }
}
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@EnableAspectJAutoProxy(proxyTargetClass=true, exposeProxy=true)
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
package com.example.demo;

import com.example.demo.inter.CustomerService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    CustomerService customerService;

    @Test
    public void testAOP() {
        customerService.doSomething1();
    }

    @Test
    void contextLoads() {
    }

}

运行下testAOP,为啥doSomething2()没有切面效果,使用AopContext.currentProxy就可以了?

拦截器的实现原理就是动态代理,实现AOP机制。Spring 的代理实现有两种:一是基于 JDK Dynamic Proxy 技术而实现的;二是基于 CGLIB 技术而实现的。如果目标对象实现了接口,在默认情况下Spring会采用JDK的动态代理实现AOP,CustomerServerImpl正是这种情况。

JDK动态代理生成的CustomerServiceImpl的代理类翻译过来如下:

package com.example.demo;

import com.example.demo.inter.CustomerService;


public class CustomerServiceProxy implements CustomerService {

    private CustomerService customerService;

    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public void doSomething1() {
        doBefore();
        customerService.doSomething1();
        // 默认,所以不会执行doBefore
        customerService.doSomething2();
        // 加入 AopContext.currentProxy的效果,完成切面效果
        this.doSomething2();
    }

    public void doSomething2() {
        doBefore();
        customerService.doSomething2();
    }

    private void doBefore() {
        System.out.println("do some important things before...");
    }
}

这样很直观地明白为啥要使用AopContext.currentProxy了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值