用Google Guice拦截构造函数 [英] Intercept a constructor with google guice
问题描述
我想用Google guice拦截构造函数.
I want to intercept a constructor with Google guice.
我在TYPE上添加了注释标记,但是"bindInterceptor"存在一些问题
I have added my annotation mark on TYPE but I have some problem with the "bindInterceptor"
如果我写
bindInterceptor(Matchers.annotatedWith(ReactToLoad.class),
Matchers.any(),
new ReactToLoadInterceptor());
这将在每种方法上运行拦截器. (很糟糕,很正常)
This will run the interceptor on each method. (it's bad and it's normal)
bindInterceptor( Matchers.any(),
Matchers.annotatedWith(ReactToLoad.class),
new ReactToLoadInterceptor());
如果我在下面运行代码,则必须将注释放在构造函数方法上(它可以工作),但我更喜欢将此注释放在类的顶部.
If I run the code just below, I MUST put the annotation on the constructor method (it works) but I prefer to put this annotation on top of classes.
我只需要一个方法Matchers作为构造方法,但是我在使用谷歌搜索时找不到它.
I just need a method Matchers for the constuctor method but I don't find it with some googling.
你能帮我吗?
推荐答案
否,不可能拦截构造函数调用……仅是方法调用.我不知道您为什么要这样做,但是您可能想查看Guice的SPI,特别是
No, it isn't possible to intercept constructor calls... only method calls. I don't know why you want to do this, but you may want to look in to Guice's SPI, specifically InjectionListener.
这篇关于用Google Guice拦截构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!