gpt4 book ai didi

spring - 为什么Spring AOP在某些情况下会拦截 protected 方法?

转载 作者:行者123 更新时间:2023-12-04 16:42:31 31 4
gpt4 key购买 nike

我读到 Spring AOP 无法拦截私有(private)和 protected 方法,但它以一种奇怪的方式拦截它们,为什么会这样?

我有这些要拦截的功能:

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* 时拦截有没有。

谁能解释我为什么会这样?

最佳答案

因为 OP (Prateek Gupta) 似乎无法(相当不愿意)创造一点 SSCCE重现这个问题,我只是在茶歇的时候很无聊,我很快就用 Spring Boot 自己创建了一个,并且非常惊讶地发现 Spring AOP 与文档相矛盾,至少在某些情况下,当 CGLIB 代理被保护时,它与 protected 方法匹配涉及。

因此,我为自己注册了一个 Spring 的 Jira 问题跟踪器帐户,并将此回归报告为 SPR-15354。 .如果对 Spring 开发团队的答案感兴趣,您可能希望订阅该票证的更新。

更新:回答我的票的人告诉我这是一个文件问题。如SPR-1611从 2006 年开始告诉我们,这已经在 Spring 1.2.7 中有意更改,但从未在文档中找到。底线:可以通过 Spring AOP 捕获 protected 方法,这不是偶然的,但已经 12 年没有记录了。

更新 2:更新的文档文本将在下一个 Spring 版本中。今天想看固定文,引用于SPR-1611 .

关于spring - 为什么Spring AOP在某些情况下会拦截 protected 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42833096/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com