如何定义一个AspectJ切入点挑出具有特定注释类的所有构造函数? [英] How to define an aspectj pointcut that picks out all constructors of a class that has a specific annotation?

查看:571
本文介绍了如何定义一个AspectJ切入点挑出具有特定注释类的所有构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是注释:

@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation {
    String name();
}

这里有一个注解的类:

Here is one annotated class:

@MyAnnotation(name="foo")
public class ClassA {
    public ClassA() {
        // Do something
    }
}

这是第二个注解的类:

Here is a second annotated class:

@MyAnnotation(name="bar")
public class ClassB {
    public ClassB(String aString) {
        // Do something
    }
}

我要寻找一个AspectJ切入点是正确的ClassA和ClassB的构造器相匹配,而不是匹配任何其他构造函数用于任何其他类通过 MyAnnotation 没有被标注。

推荐答案

您的切入点应该是这样的:

Your pointcut should look like this:

execution((@MyAnnotation *).new(..))

如果注释另外一个软件包:

If the annotation is in another package:

execution((@de.scrum_master.aop.demo.MyAnnotation *).new(..))

或者,如果你不想完全限定包:

Or if you do not want to fully qualify the package:

execution((@*..MyAnnotation *).new(..))


编辑:好吧,一些有关在评论你的问题更多的信息:


Okay, some more info about your question in the comment:

构造执行都没有,你可以在捕捉返回值

Constructor executions have no return value which you could capture in

after() returning(Object myObject) : myJoinpoint()

这仅适用于方法。所以,请用

This only works for methods. So please use

after(Object myObject) returning : myJoinpoint() && this(myObject)

而不是如果你需要任何目的构造的对象。

instead if you do need the constructed object for any purpose.

这篇关于如何定义一个AspectJ切入点挑出具有特定注释类的所有构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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