Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Inherit Annotation from Interface for pointcutting

  1. #1
    Join Date
    Sep 2008
    Posts
    7

    Default Inherit Annotation from Interface for pointcutting

    Hi!

    I would like to annotate my business interfaces so I can define pointcuts for them. Something like:

    Code:
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface BusinessInterface {
    }
    
    @BusinessInterface
    public interface MyBusinessInterface {
        public void myBusinessMethod();
    }
    
    public class MyBusinessImpl implements MyBusinessInterface {
        public void myBusinessMethod() {
            ...
        }
    }
    
    @Pointcut("@within(BusinessInterface)")
    protected void businessInterface() {
    }
    
    @Around("businessInterface()")
    protected Object myBusinessInterfaceCall(ProceedingJoinPoint joinPoint) throws Throwable {
        ...
    }
    Too bad that annotations on interfaces are not inheritable... only class annotations can!!!

    Is there any way I can still define a pointcut for all implementations of an interface?!?

    I think this is not such an esoteric requirement, is it?!?


    Kind regards!

  2. #2
    Join Date
    Nov 2007
    Posts
    420

    Default

    execution(* com.xxx.xxx.BusinessInterface+.*(..)) should do it.

  3. #3
    Join Date
    Sep 2008
    Posts
    7

    Default

    Hi bdangubic,

    I don't understand what the "+" means, it's obviously not in the Spring doc.

    But while specifying the interface directly like in ...
    Code:
        @Pointcut("execution(* com.xxx.xxx.MyBusinessInterface.*(..))")
    ... works, simply changing it to ...
    Code:
        @Pointcut("execution(* com.xxx.xxx.BusinessInterface+.*(..))")
    ... doesn't work ;-(

    Any ideas, as to why?!?


    Kind Regards

  4. #4
    Join Date
    Nov 2007
    Posts
    420

    Default

    the extra + is for matching any classes that implement the interface MyBusinessInterface. not sure why it does not work for you though...

  5. #5
    Join Date
    Sep 2008
    Posts
    7

    Default

    If I understand you correctly, then this would work:

    Code:
    @Pointcut("execution(* com.xxx.xxx.MyBusinessInterface+.*(..))")
    But what I want is one more indirection: MyBusinessImpl implements MyBusinessInterface which in turn is annotated as BusinessInterface, and I want my pointcut to match all implementations of all interfaces annotated to be BusinessInterfaces.

    I could use a naming convention like this:

    Code:
    @Pointcut("execution(* com.xxx.xxx.*BusinessInterface+.*(..))")
    But that would make the names of my business interfaces quite long winded. And eventually I would have to fetch the values of the annotation by hand.

  6. #6
    Join Date
    Dec 2007
    Location
    Belgium
    Posts
    24

    Default

    Why don't you annotate your business classes instead of your business interfaces?

    This should work:

    @BusinessInterface
    public class MyBusinessImpl implements MyBusinessInterface {
    public void myBusinessMethod() {
    ...
    }
    }

  7. #7
    Join Date
    Sep 2008
    Posts
    7

    Default

    I had been thinking of that first, by I may want to have different implementations for the same interface.

  8. #8
    Join Date
    Dec 2007
    Location
    Belgium
    Posts
    24

    Default

    So? You'll have to annotate all your different implementations.

  9. #9
    Join Date
    Dec 2007
    Location
    Belgium
    Posts
    24

    Default

    Try this

    Code:
    @Pointcut("this(BusinessInterfaceMarker)")
    protected void businessInterface() {}
    
    public interface MyBusinessInterface extends BusinessInterfaceMarker {
    	public void myBusinessMethod();
    }
    Now all method that belong to a BusinessMarkerInterface get adviced.

  10. #10
    Join Date
    Sep 2008
    Posts
    7

    Default

    Right now I'm annotating all implementations, but it is not as elegant as I would have liked it to be. Making the BusinessInterface not an annotation but a base interface as you suggesed works as well, but then I don't have an annotation object with values that I can access.

    Nevertheless: Thanks for all the ideas... I guess there is simply no super-elegant solution (yet?).

    Kind regards

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •