How to create an aspect on an Interface Method that extends from A "Super" Interface
Asked Answered
S

3

14

I have a service-layer Interface that extends from a base Interface; I would like to create a Pointcut around my service-layer Interface, but on one of the methods defined in the base Interface.

For instance.... I have a method in my base Interface called "save()", I put it in my base Interface since just all of my "child" Interfaces will provide "save" functionality.

I would like to create a PointCut on only one of my "child" interfaces for when my "save" gets called.

I created the pointcut like the following:

@Pointcut("execution(* com.xyz.someapp.ChildServiceInterface.save(..))")  
public void childServiceSavePointCut();

I then created a @Around advice around the above pointcut like the following:

@Around("childServiceSavePointCut()")
public void doMyAdvice()....

where "ChildServiceInterface" extends another Interface which has the "save()" method defined.

My Advice never runs... I debugged my code and do not see my Advice in the list of Advisors for my target service.

Am I way off base thinking this will work, or am I implementing it incorrectly?

Symmetry answered 10/2, 2011 at 14:35 Comment(0)
L
26

Try this pointcut instead.

within(com.xyz.someapp.ChildServiceInterface+) && execution(* save(..))

The + indicates a subtype pattern.

Lymphocyte answered 10/2, 2011 at 14:54 Comment(0)
I
1

Or you can put pointcut on all the methods of that class using

@Pointcut("execution(* com.xyz.someapp.ChildServiceInterface.*(..))")  
public void childServiceSavePointCut();

The * indicates all method type.

Iinde answered 2/7, 2017 at 8:38 Comment(0)
C
0

This one helps me:

    @Pointcut(
        "@within(com.xyz.someapp.ChildServiceInterface) && execution(* save(..))"
    )

Copula answered 27/5, 2022 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.