Android Roboguice Use Guide (one) scopes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.