【问题标题】:AspectJ - pointcut for methods of interface's implementationAspectJ - 接口实现方法的切入点
【发布时间】:2017-12-25 09:01:38
【问题描述】:

我有几个 SomeInterface 的实现。问题是在 SomeInterface 的所有实现中方法 executeSomething 的切入点是什么。

public class SomeImplementation implements SomeInterface {

    public String executeSomething(String parameter) {
        // Do something
    }

}

public class AnotherImplementation implements SomeInterface {

    public String executeSomething(String parameter) {
        // Do something different way
    }

}

【问题讨论】:

  • 您真的对接口的所有实现感兴趣吗,难道“所有类都具有名称为 X 的方法”就足够了吗?如果是这样,您可以使用@Pointcut("execution(* *.*.executeSomething(..))"),对吗?
  • "all classes have method with name X" 如果没有其他类(不是接口的实现),则可以。感谢您的建议,但理想情况下我需要所有实现

标签: java aspectj


【解决方案1】:

该方法的切入点可以是方法执行或方法调用切入点。根据您的要求,最具体的切入点如下所示:

execution(public String SomeInterface+.executeSomething(String))
call(public String SomeInterface+.executeSomething(String))

对这些切入点类型的一些解释:

  • 在这两个切入点中使用的类型模式意味着:所有返回 String 的公共方法在 SomeInterface 或其任何子类型中定义,命名为 executeSomething 并接受单个 String 参数。这是可以为您的案例定义的最具体的类型模式,它将仅匹配 String SomeInterface.executeSomething(String) 方法的实现。
  • 执行类型切入点与执行特定方法主体时对应的连接点匹配
  • 调用类型切入点匹配与调用特定方法时对应的连接点(即连接点位于调用方一侧)

执行类型的切入点使用较多,但调用类型的切入点在某些情况下也非常有用。

请参阅AspectJ Programming Guide 中的The AspectJ Language/Join Points and Pointcuts 章节以获取更多参考。

【讨论】:

    猜你喜欢
    • 2019-12-31
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2014-11-17
    • 1970-01-01
    • 2011-09-29
    相关资源
    最近更新 更多