How to define an aspectj pointcut that picks out all constructors of a class that has a specific annotation?

前端 未结 3 1336
一向
一向 2021-01-20 18:20

Here is the annotation:

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


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-20 19:18

    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(..))
    

    Edit: 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.

提交回复
热议问题