1
看來Guice忽略了我的模塊的@Provider方法。guice忽略提供者?
我有一個類MyModule的是這樣的:
public class MyModule extends AbstractModule {
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(Timed.class), new GuiceEnabledLoggingInterceptor());
bind(OneClass.class).to(OneClassImpl.class);
// And more binding lines...
}
@Provides
public AnotherClassInApi provideMyClass() {
return AnotherClassInApi.getInstance();
}
// And more @Provides methods
}
主要方法是
public static void main(String[] args){
ConfigHandler.getInstance().loadConfigWhenNotRunningInsideMicrocontainer();
Injector INJECTOR = Guice.createInjector(new MyModule());
// ...
}
在項目的其他部分我上課AnotherClassInApi,這是一個非常標準的單加一個方法:
public class AnotherClassInApi {
private static final AnotherClassInApi INSTANCE = new AnotherClassInApi();
private AnotherClassInApi() { }
// ... more methods
public static AnotherClassInApi getInstance() {
return INSTANCE;
}
}
那麼,我明白,應該有效地綁定任何請求的A notherClassInApi對象到getInstance()方法,但它不起作用。有趣的是,在調試時,@Provide方法中的斷點永遠不會到達,但在配置方法中達到了一個斷點。看起來guice忽略了我的提供商註釋,我認爲I'm following exactly what Guice guide says about @Provider,所以我已經卡住了。
我一直在搜索,但找不到類似的東西。任何幫助都感激不盡。
謝謝!
你的實際'@ Inject'點看起來像你要求一個'AnotherClassInApi'的實例嗎? – condit 2012-07-30 15:36:24