AspectJ匹配返回类型作为泛型的接口

时间:2015-06-05 14:54:29

标签: aspectj

我正在尝试创建一个AspectJ Aspect来拦截具有通用接口的返回方法。

这是我的AspectJ

@AspectJ
public class MyAspect {

    @AfterReturning("execution(java.util.Collection+<mypackage.MyAbstractEntity+> mypackage.mvc.controllers..*.*(..))", returning = "list")
    public doStuff(JoinPoint j, Collection<MyAbstractEntity> list) {
    }
}

这是我想编入的课程:

package mypackage.mvc.controller;

public class MyController {
    // MyEntity extends MyAbstractEntity
    public List<MyEntity> findAll() {
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:4)

解决!

把&#34;加&#34;在泛型定义之后(&#34;加&#34;表示&#34;扩展它的类&#34;):

java.util.Collection<mypackage.MyAbstractEntity+>+

并签署&#34;列表&#34;作为&#34;?延伸&#34;:

public doStuff(JoinPoint j, Collection<? extends MyAbstractEntity> list) {

代码如下所示:

@AspectJ
public class MyAspect {

    @AfterReturning("execution(java.util.Collection<mypackage.MyAbstractEntity+>+ mypackage.mvc.controllers..*.*(..))", returning = "list")
    public doStuff(JoinPoint j, Collection<MyAbstractEntity> list) {
    }
}