私は Guice と AspectJ を使用しており、特定のメソッドの実行時間を測定するために AOP を実行しようとしています。
測定する必要があるすべてのメソッドに注釈を付けるために使用されるこの注釈があります。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
public @interface MyStopWatch {
}
私はこのメソッドインターセプターを持っています:
public class MyInterceptor implements org.aopalliance.intercept.MethodInterceptor {
private final Logger logger = LoggerFactory.getLogger(MyInterceptor.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
final org.apache.commons.lang3.time.StopWatch stopWatch = org.apache.commons.lang3.time.StopWatch.createStarted();
final Object returnedObject = invocation.proceed();
stopWatch.stop();
logger.info("[STOPWATCH] Method: {} - Time: {}.", invocation.getMethod().getName(), stopWatch.getTime());
return returnedObject;
}
}
私はこのインターフェースを持っています
public interface MySuperClass {
@MyStopWatch
default void test() {
System.out.println("Hello, world");
}
}
次に、MySuperClass から継承するこのサブクラスがあります。
public class MySubClass implements MySuperClass {
}
そして最後に、私はこのバインドを持っています:
public class MyAOPModule extends AbstractModule {
@Override
protected void configure() {
bindInterceptor(
Matchers.any(),
Matchers.annotatedWith(MyStopWatch.class),
new MyInterceptor()
);
}
}
Guice モジュールを次のように初期化します。
public static void main(String[] args) throws Throwable {
Injector injector = createInjector(new MyAOPModule());
injector.getInstance(MySubClass.class).test();
}
私の問題は、サブクラスの test() メソッドの実行に注釈が付けられていないかのように、ログに何も記録されていないことです。
これを回避する方法はありますか?