springboot aop无效的情况

项目结构
springboot aop无效的情况_第1张图片

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了。

你可能感兴趣的:(java)