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

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

问题描述

我读到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?

我想拦截这些功能:

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:更新的文档文本将在下一春季发行版中.如果您想今天阅读固定文本,请在 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会拦截受保护的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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