我有一种情况,Guice正在为某些绑定工作,而对其他绑定则根本没有。显然我错误地使用了API。
在某种程度上,这可能是因为我试图对我如何使用Guice过于“幻想”。我已经创建了一个Module
s的继承树,我认为为了自己的利益我已经变得太聪明(或愚蠢!)。
在您查看下面的代码之前,请理解我的意图,这是为了提供一个可重用的Module
,我可以放在JAR中并在多个项目中共享。这个抽象的,可重用的Module
将提供所谓的“默认绑定”,任何实现Module
都会自动兑现。类似于MethodInterceptor
的AOP Profiler
,它查找用@Calibrated
注释的方法,并自动记录该方法执行所需的时间等。
请注意以下事项:
@Target({ ElementType.METHOD })
@RetentionPolicy(RetentionPolicy.RUNTIME)
@BindingAnnotation
public @interface Calibrated{}
public class Profiler implement MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
// Eventually it will calculate and log the amount of time
// it takes an intercepted method to execute, hence "Profiler".
System.out.println("Intercepted!");
return arg0.proceed();
}
}
public abstract class BaseModule implements Module {
private Binder binder;
public BaseModule() {
super();
}
public abstract void bindDependencies();
@Override
public void configure(Binder bind) {
// Save the binder Guice passes so our subclasses can reuse it.
setBinder(bind);
// TODO: For now do nothing, down the road add some
// "default bindings" here.
// Now let subclasses define their own bindings.
bindDependencies();
}
// getter and setter for "binder" field
// ...
}
public abstract class AbstractAppModule extends BaseModule {
/* Guice Injector. */
private Injector injector;
// Some other fields (unimportant for this code snippet)
public AbstractAppModule() {
super();
}
// getters and setters for all fields
// ...
public Object inject(Class<?> classKey) {
if(injector == null)
injector = Guice.createInjector(this);
return injector.getInstance(classKey);
}
}
所以,要使用这个小型库:
public class DummyObj {
private int nonsenseInteger = -1;
// getter & setter for nonsenseInteger
@Calibrated
public void shouldBeIntercepted() {
System.out.println("I have been intercepted.");
}
}
public class MyAppModule extends AbstractAppModule {
private Profiler profiler;
// getter and setter for profiler
@Override
public void bindDependencies() {
DummyObj dummy = new DummyObj();
dummy.setNonsenseInteger(29);
// When asked for a DummyObj.class, return this instance.
getBinder().bind(DummyObj.class).toInstance(dummy);
// When a method is @Calibrated, intercept it with the given profiler.
getBinder().bindInterceptor(Matchers.any(),
Matchers.annotatedWith(Calibrated.class),
profiler);
}
}
public class TestGuice {
public static void main(String[] args) {
Profiler profiler = new Profiler();
MyAppModule mod = new MyAppModule();
mod.setProfiler(profiler);
// Should return the bounded instance.
DummyObj dummy = (DummyObj.class)mod.inject(DummyObj.class);
// Should be intercepted...
dummy.shouldBeIntercepted();
System.out.println(dummy.getNonsenseInteger());
}
}
这是很多代码所以我可能在将其全部输入时引入了一些拼写错误,但我向你保证,这些代码会在运行时编译并抛出没有异常。
以下是发生的事情:
@Calibrated shouldBeIntercepted()
方法未截获;但是... 因此,无论您可能认为这是多么糟糕,您都不能认为Guice确实在为1个绑定(实例绑定)工作,而不是AOP方法拦截。
如果实例绑定不起作用,那么我很乐意重新审视我的设计。但是这里还有别的东西。我想知道我的继承树是否以某种方式抛出Binder
?
我已经验证我正在将拦截器正确绑定到注释:我创建了另一个包,我只是实现Module
(而不是这个继承树)并使用相同的注释,相同的{{1它完美无缺。
我使用Profiler
打印出我所有Injector.getAllBindings()
绑定的地图作为字符串。没有任何东西可以作为这个错误的明确来源。
有什么想法吗?
答案 0 :(得分:7)
拦截仅适用于Guice创建的对象(请参阅“限制”http://code.google.com/p/google-guice/wiki/AOP#Limitations)。您正在使用“new”来创建DummyObj,因此无论您的模块设置多么聪明,该实例都是在guice下创建的。
根据您的编码,这是一个小小的宣传片。 (我使用你的Calibrated Annotation,但是在一个类中有其他所有内容。你应该看一下“AbstractModule”。它可以节省你在Module-Hierarchy中做的很多手工操作。
public class MyModule extends AbstractModule implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Intercepted@invoke!");
return methodInvocation.proceed();
}
@Override
protected void configure() {
bind(Integer.class).annotatedWith(Names.named("nonsense")).toInstance(29);
bindInterceptor(Matchers.any(), Matchers.annotatedWith(Calibrated.class), this);
}
public static void main(String... args) {
Dummy dummy = Guice.createInjector(new MyModule()).getInstance(Dummy.class);
dummy.doSomething();
System.out.println(dummy.getNonsense());
}
}
我的假人:
public class Dummy {
@Inject
@Named("nonsense")
private int nonsense;
public int getNonsense() {
return nonsense;
}
public void setNonsense(int nonsense) {
this.nonsense = nonsense;
}
@Calibrated
public void doSomething() {
System.out.println("I have been intercepted!");
}
}
所以你看到了吗?我从不使用“新”这个词(模块除外)。我让Guice处理Dummy-Object,然后配置nonsense int的值,然后注入。
输出:
Intercepted@invoke!
I have been intercepted!
29