小编Vai*_*hal的帖子

Guice 方法拦截器不工作

注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD})
public @interface PublishMetric {
}
Run Code Online (Sandbox Code Playgroud)

拦截器

public class PublishMetricInterceptor implements MethodInterceptor {
  @Override
  public Object invoke(MethodInvocation methodInvocation) throws Throwable {
    System.out.println("invoked");
    return methodInvocation.proceed();
  }
}
Run Code Online (Sandbox Code Playgroud)

指南模块

public class MetricsModule extends AbstractModule {
  @Override
  protected void configure() {
    bindInterceptor(any(), annotatedWith(PublishMetric.class), new PublishMetricInterceptor());
  }

  @Provides
  @Singleton
  public Dummy getDummy(Client client) {
    return new Dummy(client);
  }
}
Run Code Online (Sandbox Code Playgroud)

用法

public class Dummy {
  private final Client client;

  @Inject
  public Dummy(final Client client) {
    this.client = client;
  }

  @PublishMetric
  public String …
Run Code Online (Sandbox Code Playgroud)

java aop guice

3
推荐指数
1
解决办法
3876
查看次数

标签 统计

aop ×1

guice ×1

java ×1