可能已经晚了,但是我遇到了一些可以很好地解释您与代理相关的问题的信息(只有通过代理进入的“外部”方法调用会被拦截)。
例如,您有一个看起来像这样的班级
@Component("mySubordinate")
public class CoreBusinessSubordinate {
public void doSomethingBig() {
System.out.println("I did something small");
}
public void doSomethingSmall(int x){
System.out.println("I also do something small but with an int");
}
}
并且您有一个方面,看起来像这样:
@Component
@Aspect
public class CrossCuttingConcern {
@Before("execution(* com.intertech.CoreBusinessSubordinate.*(..))")
public void doCrossCutStuff(){
System.out.println("Doing the cross cutting concern now");
}
}
当您像这样执行它时:
@Service
public class CoreBusinessKickOff {
@Autowired
CoreBusinessSubordinate subordinate;
// getter/setters
public void kickOff() {
System.out.println("I do something big");
subordinate.doSomethingBig();
subordinate.doSomethingSmall(4);
}
}
在上面给定代码上方调用kickOff的结果。
I do something big
Doing the cross cutting concern now
I did something small
Doing the cross cutting concern now
I also do something small but with an int
但是当您将代码更改为
@Component("mySubordinate")
public class CoreBusinessSubordinate {
public void doSomethingBig() {
System.out.println("I did something small");
doSomethingSmall(4);
}
public void doSomethingSmall(int x){
System.out.println("I also do something small but with an int");
}
}
public void kickOff() {
System.out.println("I do something big");
subordinate.doSomethingBig();
//subordinate.doSomethingSmall(4);
}
您会看到,该方法在内部调用了另一个方法,因此不会被截取,并且输出将如下所示:
I do something big
Doing the cross cutting concern now
I did something small
I also do something small but with an int
您可以通过这样做来绕过
public void doSomethingBig() {
System.out.println("I did something small");
//doSomethingSmall(4);
((CoreBusinessSubordinate) AopContext.currentProxy()).doSomethingSmall(4);
}
摘录自以下网址的代码段:https:
//www.intertech.com/Blog/secrets-of-the-spring-aop-proxy/