【发布时间】:2017-03-16 11:40:29
【问题描述】:
我读到 Spring AOP 无法拦截私有和受保护的方法,但它以一种奇怪的方式拦截它们,为什么会这样?
我有这些函数要拦截:
public String getName(String string) {
System.out.println("Name : " + name + string);
return name;
}
protected String getNamesprotected(String string) {
System.out.println("Name : " + name + string);
return name;
}
这是我的@Aspect 代码:
@Aspect
public class Logging {
@Before("execution(* com.tutorialspoint.Student.*Name*(..))")
public void beforeAdvice(JoinPoint joinPoint){
System.out.println("Going to setup student profile."+joinPoint.getSignature().toString());
}
}
执行此代码时,getName 和 getNamesprotected 都会被拦截,但当我执行此代码时:
@Aspect
public class Logging {
@Before("execution(* com.tutorialspoint.Student.getNamesprotected(..))")
public void beforeAdvice1(JoinPoint joinPoint){
System.out.println("Going to setup student profile."+joinPoint.getSignature().toString());
}
}
然后什么都不会被截获。我也尝试用*getNamesprotected* 替换getNamesprotected,但它仍然没有拦截。它仅在*Name* 存在时拦截。
谁能解释一下为什么会这样?
【问题讨论】:
-
除非您使用 AspectJ 而不是 Spring AOP,否则您所描述的很难相信。我只是觉得你犯了一个错误。 Spring AOP 永远不会拦截受保护的方法。如果您仍然认为是这样,请在 GitHub 上使用 SSCCE 证明这一点。那我愿意去看看。
-
你能举一个SSCCE的例子吗?我是新来的,所以不完全知道如何创建。
标签: spring aop aspectj spring-aop