如何将方法拦截器绑定到提供程序?

Usm*_*ail 12 java guice

有没有办法将方法拦截器绑定到提供程序而不是实例?

例如,我使用下面的代码绑定拦截器如何将INTERCEPTOR绑定到提供程序,然后绑定到注释?

bindInterceptor(
    Matchers.any(), Matchers.annotatedWith(ANNOTATION.class), new INTERCEPTOR());
Run Code Online (Sandbox Code Playgroud)

Joh*_*sen 18

Guice不允许在非Guice构建的实例上使用AOP: Guice AOP限制

"实例必须由Guice通过@Inject-annotated或无参数构造函数创建"

这意味着使用提供程序创建的实例不会成为AOP的候选者.

另一方面,只要您的提供商在提到的条件下由Guice实例化,您的提供商可能是AOP的候选人.

这是一个演示这个的例子:

AOP注释:

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

提供者:

public class ExampleProvider implements Provider<Example> {

    @AOPExample
    public Example get() {
        System.out.println("Building...");
        return new Example();
    }
}
Run Code Online (Sandbox Code Playgroud)

目标示例:

public class Example {

    @AOPExample
    public void tryMe() {
        System.out.println("example working...");
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

模块:

public class ExampleModule extends AbstractModule {
    @Override
    protected void configure() {
        bindInterceptor(Matchers.any(), Matchers.annotatedWith(AOPExample.class), new LoggingAOP());

        bind(Example.class).toProvider(ExampleProvider.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

测试代码:

public class Test {

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new TestModule());

        ExampleProvider exampleProvider = injector.getInstance(ExampleProvider.class);
        Example example = exampleProvider.get();

        example.tryMe();

        Example directExample = injector.getInstance(Example.class);

        directExample.tryMe();

    }
}
Run Code Online (Sandbox Code Playgroud)

测试输出:

start
Building...
end took: 3
example working...
start
Building...
end took: 0
example working...
Run Code Online (Sandbox Code Playgroud)

请注意,"示例工作..."未被定时器代码包围.然而,Provider.get("Building ...")是.

如果您的问题是:拦截器(新的INTERCEPTOR())是否可以通过Guice Provider提供,答案是否定的.您可以获得此功能的最接近的方法是在模块配置方法中调用requestInjection().这将为您的Interceptor注入适当的代码.从您的拦截器,您可以使用提供程序,以避免任何导致您在启动过程中缓慢的开销.

这就是我的意思:

模块:

public class TestModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(String.class).toInstance("One");
        bind(String.class).annotatedWith(Names.named("two")).toInstance("Two");

        LoggingAOP loggingAOP = new LoggingAOP();

        bindInterceptor(Matchers.any(), Matchers.annotatedWith(AOPExample.class), loggingAOP);

        requestInjection(loggingAOP);

        bind(Example.class).toProvider(ExampleProvider.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

拦截器:

public class LoggingAOP implements MethodInterceptor {

    @Inject
    private Provider<SomethingThatTakesALongTimeToInit> provider;

    public Object invoke(MethodInvocation invocation) throws Throwable {
        provider.get()...
        System.out.println("start");
        long start = System.currentTimeMillis();
        Object value =  invocation.proceed();
        System.out.println("end took: " + (System.currentTimeMillis() - start));
        return value;
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这能回答你的问题.