By default, Guice creates a new instance object of the class each time to the place where the class instance is needed. You can use scopes to modify this default behavior, and scope allows class instances to be reused within a certain range. Roboguice are commonly used in two ways:
Use the same instance object @Singleton the entire application lifecycle
Share an Instance object @ContextScoped the same context (such as an activity).
Use the scope method to use the appropriate markup, such as:
@Singleton public
class Inmemorytransactionlog implements TransactionLog {
//Everything here should is threadsafe!
}
Or use the BIND statement in module:
Bind
(Transactionlog.class)
. to (Inmemorytransactionlog.class)
. In (Singleton.class);
If you use @provides, you can have:
@Provides @Singleton
TransactionLog providetransactionlog () {
...
}
If a type uses a scope tag that you do not want to use, you can bind it to Scopes.no_scope to cancel the scope definition.
View a full set of articles: Http://www.bianceng.cn/OS/extra/201301/34950.htm