我自定义了一个注解@MyAnnotation
配置了一个切面,计划是当运行有@MyAnnotation注解的方法前打印一下信息
@Before(value = "@annotation(com.ming.annotation.MyAnnotation)")
public void testInterceptor(){
System.out.println("=================testInterceptor=============");
}
有一个类test.java有两个方法
public class UserService {
@MyAnnotation
public String addUser(User user){
test();
return user.save();
}
@MyAnnotation
public void test(){
}
}
当我调用addUser方法的时候,打印了一次"=================testInterceptor============="
但是没有打印第二次test方法的"=================testInterceptor============="
为什么?
------解决思路----------------------
拿你这个例子来说
addUser方法里test();方法我们还原成完整写法是this.test().
而这个this是指的UserService当前这个类实例。
你在外部调用userservice.addUser() 实际上是从spring容器里拿的一个UserService的代理。我们姑且叫UserServiceProxy。这个代理里包含了你的切面代码。
也就是userServiceProxy.addUser();这个切面就会生效
而addUser方法里的调用test() 就实际上是userService.test()
并不是代理类调用的,自然就没了切面了