动态代理方法互调,静态成员类

在日常使用,还有面试中,经常会涉及到AOP的相关知识,AOP虽好,但是有一些小的细节注意不到,可能会被坑;

1.动态代理类的方法互调,被调用的方法会不会生成代理?

2.静态成员类,调用过程中会不会生成代理?

对以上这些问题进行测试:

首页,这是AOP切面代码

@Component
@Aspect
public class AopTest {

    @Pointcut("execution(public * com.wuzhiaite.javaweb.spring.serveice.AopServiceTest.*(..))")
    public void AopTest(){ }
@Before(
"AopTest()") public void doBefore(){ System.out.println("doBefore"); }
@After(
"AopTest()") public void doAfter(){ System.out.println("doAfter"); } }

 接下来:被代理类的代码

@Service
public class AopServiceTest {

    public void testMethod(){
        new Inner().innerMethod();
        System.out.println("testMethod");
        this.testMethod2();
    }
    public void testMethod2(){
        System.out.println("testMethod22222222");
    }

    public static class Inner{
        public void innerMethod(){
            System.out.println("innerMethod");
        }
    }

}

再接下来,测试以下:

@RunWith(SpringJUnit4ClassRunner.class)
@Slf4j
@EnableAspectJAutoProxy
public class SpringBootJavawebBaseApplicationTests {

    @Autowired
    private AopServiceTest service;

    @Test
    public void aopTest1(){
        service.testMethod();
        new AopServiceTest.Inner().innerMethod();//静态内部类并不会生成代理类
    }
}

测试出来的结果是:

 很显然,静态成员类和方法内部调用的方法并没有生成代理,至于原因,后面康康源码再补充

猜你喜欢

转载自www.cnblogs.com/perferect/p/12619956.html
今日推荐