为什么Spring AOP在某些情况下会拦截protected方法? [英] Why does Spring AOP intercept protected methods under certain circumstances?

查看:69
本文介绍了为什么Spring AOP在某些情况下会拦截protected方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到 Spring AOP 不能拦截私有和受保护的方法,但它以一种奇怪的方式拦截它们,这是为什么?

I read that Spring AOP cannot intercept private and protected methods but it is intercepting them in a weird way why is that?

我有这些要拦截的函数:

I have these functions i want to intercept:

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 都被拦截,但是当我执行此代码时:

When this code is executed both getName and getNamesprotected are intercepted but when I execute this code :

@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* 存在时拦截.

Then nothing is intercepted. I also tried replacing getNamesprotected with *getNamesprotected* but still it does not intercept. It only intercepts when *Name* is there.

谁能解释一下为什么会这样?

Can anyone explain me why this is happening?

推荐答案

因为 OP (Prateek Gupta) 似乎无法(相当不愿意)创建一个小SSCCE 重现了这个问题,我只是在茶歇期间感到无聊,我很快用 Spring Boot 自己创建了一个,并且很惊讶确实 Spring AOP,至少与文档相矛盾当涉及CGLIB代理时,在某些情况下与受保护的方法匹配.

Because the OP (Prateek Gupta) seemed to be unable (rather unwilling) to create a little SSCCE reproducing the problem and I was just bored during a tea break, I quickly created one by myself with Spring Boot and was very surprised that indeed Spring AOP, in contradiction to the documentation, at least under certain circumstance matches against protected methods when CGLIB proxies are involved.

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

Thus, I registered myself an account for Spring's Jira issue tracker and reported this regression as SPR-15354. You might want to subscribe for updates on that ticket if interested in answers by the Spring development team.

更新:回答我的票的人告诉我这是一个文档问题.正如 2006 年的 SPR-1611 告诉我们的那样,Spring 1.2 已经有意更改了这一点.7 但从未在文档中找到它的方式.底线:受保护的方法可以通过 Spring AOP 捕获,这不是偶然的,但 12 年来一直没有记录.

Update: The person answering to my ticket told me that this is a documentation issue. As SPR-1611 from 2006 tells us, this was already changed on purpose for Spring 1.2.7 but has never found its way into the documentation. Bottom line: Protected methods can be captured via Spring AOP, it is not an accident but has been undocumented for 12 years.

更新 2:更新的文档文本将在下一个 Spring 版本中发布.如果今天要阅读固定文本,在SPR-1611中引用.

Update 2: The updated documentation text will be in the next Spring release. If you want to read the fixed text today, it is quoted in SPR-1611.

这篇关于为什么Spring AOP在某些情况下会拦截protected方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆