0

I want to add API versions across all log statements. To achieve that, I have created a custom annotation and its corresponding interceptor.

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.PACKAGE})
@Around
@Documented
public @interface LogAPIVersion {
    String apiVersion() default "";
}

Things are working fine if I place annotation on a method or a class.

But, I want to apply this annotation at a package level (for all classes and their methods). I've created a package-info.java file and decorated my custom annotation onthe package name.

@LogAPIVersion(apiVersion = "v1")
package com.example.controllers.v1;

Unfortunately, my logging interceptor is not getting invoked. How do I make this work?

goutham
  • 624
  • 1
  • 6
  • 8

2 Answers2

1

The simple answer is: Neither native AspectJ nor simpler AOP frameworks such as Spring AOP provide any means to intercept package-level annotations.

What you could do is use native AspectJ's annotation processing support in order to generate annotations for all classes or methods you wish to target during your build and then intercept them using an aspect. Here are some of my old answers showing examples of how to use the feature:

kriegaex
  • 63,017
  • 15
  • 111
  • 202
0

It might need to be annotated with @Inherited.

Denis
  • 603
  • 4
  • 8
  • Tried it, but it's not working. – goutham Oct 18 '22 at 18:22
  • `@Inherited` annotations only have an effect on class types, not on interface types, methods, parameters, fields or packages. See for example [my answer here](https://stackoverflow.com/a/42607016/1082681). – kriegaex Nov 01 '22 at 08:45