Guice (3.0) に関するいくつかの記事とチュートリアルを読みましたが、「すべてを結び付ける」前にいくつかの疑問が残ります。
// 1. Binds via public, no-arg "ServiceImpl()" ctor?
bind(Service.class).to(ServiceImpl.class);
// 2. Every client-side request for a Service instance returns the same
// ServiceImpl instance?
ServiceImpl impl = new ServiceImpl(...);
bind(Service.class).toInstance(impl);
// 3. Every client-side request for a Service instance returns the same
// SINGLETON ServiceImpl instance?
ServiceImpl impl = new ServiceImpl(...);
bind(Service.class).in(Scopes.SINGLETON).toInstance(impl);
// 4. Should this be a call too bindConstant() instead of toInstance()
// instead? If so, how/why?
Integer timeout = 1000 * 60; // 60 seconds
bind(Integer.class).named(Names.named("TIMEOUT")).toInstance(timeout);
上記のコード スニペットに示されているように、私の質問は次のとおりです。
- を使用
to(...)
する場合、public no-arg ctor が使用され、毎回新しいインスタンスが返されると仮定しますか? - 上記の #2 に従って、
impl
これまでのリクエストで同じインスタンスが使用されていますService.class
か、それとも新しいインスタンスが返されていますか? - 上記の #3 と同じですが、
Scopes.SINGLETON
指定されています。 - 上記のコードは問題ありませんか、それとも使用する必要があり
bindConstant()
ますか? もしそうなら、どのように/なぜですか? - どのような状況で、いわゆるプロバイダー メソッドを使用する必要がありますか? 私はそのページの例をある程度理解していますが、私のコードでそれらの実際の使用例を見つけることになると、窒息しています.
前もって感謝します!