Scope of Google guice

Source: Internet
Author: User
By default, when guice gets an instance, a new object is returned each time. This behavior can be configured through scopes. Scopes allows you to reuse instances:
The entire application lifecycle (@ Singleton), Session (@ Session), request (@ requestscoped), guice also provides a servlet extended scope for Web applications. You can also customize the range in guice.

Guice uses annotations to identify the scope and adds annotations to a certain type of implementation class:


@Singletonpublic class InMemoryTransactionLog implements TransactionLog {  /* everything here should be threadsafe! */}

The scope can also be configured using the BIND statement:


bind(TransactionLog.class).to(InMemoryTransactionLog.class).in(Singleton.class);

@ Provides Method Add scope annotation:

@Provides @SingletonTransactionLog provideTransactionLog() {...}

If the annotation used conflicts with the BIND () statement during configuration scope, the configuration in BIND () prevails. You can use
Scopes. no_scope.
In link binding, the scope is applied to the binding source, not to the binding target. Suppose we have an appleess class that implements the Bar and Grill interfaces,
The following binding configuration has two instances, one for bar and the other for grill:

bind(Bar.class).to(Applebees.class).in(Singleton.class);bind(Grill.class).to(Applebees.class).in(Singleton.class);

This is because the scope is applied to the binding source (Bar, grill) instead of the binding target (appless). If you want to create only one instance, you can add the @ Singleton annotation to the appless class, or add another binding Configuration:

bind(Applebees.class).in(Singleton.class);
This binding configuration makes the two. In (singleton. Class) Statements redundant. The IN () statement can receive not only a scope annotation, such as requestscope. class, but also a scope instance, such as servletscopes. Request:

bind(UserPreferences.class)      .toProvider(UserPreferencesProvider.class)      .in(ServletScopes.REQUEST);
It is more appropriate to use annotations to configure the scope, because this allows module objects to be reused in a non-type manner.

Guice has a special syntax to define the singleton object (eager singletons) to be created immediately: BIND (transactionlog. Class). To (inmemorytransactionlog. Class). aseagersingleton ();
The eager singletons object ensures the consistent experience of end users. Lazy singletons is more suitable for the edit-compile-run development cycle. You can use stage enumeration to select which policy to use.


  Production Development
. Aseagersingleton () Eager Eager
. In (singleton. Class) Eager Lazy
. In (scopes. Singleton) Eager Lazy
@ Singleton Eager * Lazy
* Indicates that a singleton object will be created immediately only for known types. The so-called passed dependency of these classes is added to the classes used in the module.

How to select a scope:
If an object is in the state, its state is obvious. Each application uses @ Singleton, and each request uses @ requestscoped. If
Objects are stateless and the Creation cost is small, so there is no need to configure the scope. guice creates a new object each time.
The Singleton mode is popular in Java applications, but it cannot provide multiple objects, especially after dependency injection. Although the singleton mode reduces object creation and promotes garbage collection, initialization of the singleton object needs to be synchronized. The singleton object is best suited:
A. stateful object. If the configuration object or counter is
B. It takes a lot of money to create or search
C. Objects bound with resources, such as database connection pools


When a class is added with the @ Singleton or @ sessionscoped annotation, it must be thread-safe. Classes injected into this class must also be secure. You should restrict the concurrency control status to minimize variability. @ Requestscoped objects do not have to be thread-safe. Therefore, a common error is that a @ Singleton or @ sessionscoped object depends on a @ requestscoped object.

Scope of Google guice

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.