- Guice - Home
- Guice - Overview
- Guice - Environment Setup
- Guice - First Application
- Bindings Examples
- Guice - Linked binding
- Guice - Binding Annotations
- Guice - @Named binding
- Guice - Constant Bindings
- Guice - @Provides Annotation
- Guice - Provider Class
- Guice - Constructor Bindings
- Guice - Inbuilt Bindings
- Guice - Just-in-time Bindings
- Injection Examples
- Guice - Constructor Injection
- Guice - Method Injection
- Guice - Field Injection
- Guice - Optional Injection
- Guice - On-demand Injection
- Miscellaneous Examples
- Guice - Scopes
- Guice - AOP
- Guice Useful Resources
- Guice - Quick Guide
- Guice - Useful Resources
- Guice - Discussion
Guice - Scopes
Introduction
Guice returns a new instance every time when it supplies a value as its default behaviour. It is configurable via scopes. Following are the scopes that Guice supports:
@Singleton − Single instance for lifetime of the application. @Singleton object needs to be threadsafe.
@SessionScoped − Single instance for a particular session of the web application. @SessionScoped object needs to be threadsafe.
@RequestScoped − Single instance for a particular request of the web application. @RequestScoped object does not need to be threadsafe.
Way to apply scopes.
Following are the ways to apply scopes.
At Class level
@Singleton
class SpellCheckerImpl implements SpellChecker {
public SpellCheckerImpl(){}
@Override
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
At Configuration level
bind(SpellChecker.class).to(SpellCheckerImpl.class).in(Singleton.class);
At Method level
@Provides @Singleton
public SpellChecker provideSpellChecker(){
String dbUrl = "jdbc:mysql://localhost:5326/emp";
String user = "user";
int timeout = 100;
SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout);
return SpellChecker;
}
Let's see the Scope at class level in action.
Example - Usage of @Singleton Annotation
GuiceTester.java
package com.tutorialspoint;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
public class GuiceTester {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new TextEditorModule());
SpellChecker spellChecker = new SpellCheckerImpl();
injector.injectMembers(spellChecker);
TextEditor editor = injector.getInstance(TextEditor.class);
System.out.println(editor.getSpellCheckerId());
TextEditor editor1 = injector.getInstance(TextEditor.class);
System.out.println(editor1.getSpellCheckerId());
}
}
class TextEditor {
private SpellChecker spellChecker;
@Inject
public void setSpellChecker(SpellChecker spellChecker){
this.spellChecker = spellChecker;
}
public TextEditor() { }
public void makeSpellCheck(){
spellChecker.checkSpelling();
}
public double getSpellCheckerId(){
return spellChecker.getId();
}
}
//Binding Module
class TextEditorModule extends AbstractModule {
@Override
protected void configure() {
bind(SpellChecker.class).to(SpellCheckerImpl.class);
}
}
interface SpellChecker {
public double getId();
public void checkSpelling();
}
@Singleton
class SpellCheckerImpl implements SpellChecker {
double id;
public SpellCheckerImpl(){
id = Math.random();
}
@Override
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
@Override
public double getId() {
return id;
}
}
Output
Compile and run the file, you may see the following output with same numbers −
0.4168185515418945 0.4168185515418945
Example - Without using @Singleton Annotation
GuiceTester.java
package com.tutorialspoint;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
public class GuiceTester {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new TextEditorModule());
SpellChecker spellChecker = new SpellCheckerImpl();
injector.injectMembers(spellChecker);
TextEditor editor = injector.getInstance(TextEditor.class);
System.out.println(editor.getSpellCheckerId());
TextEditor editor1 = injector.getInstance(TextEditor.class);
System.out.println(editor1.getSpellCheckerId());
}
}
class TextEditor {
private SpellChecker spellChecker;
@Inject
public void setSpellChecker(SpellChecker spellChecker){
this.spellChecker = spellChecker;
}
public TextEditor() { }
public void makeSpellCheck(){
spellChecker.checkSpelling();
}
public double getSpellCheckerId(){
return spellChecker.getId();
}
}
//Binding Module
class TextEditorModule extends AbstractModule {
@Override
protected void configure() {
bind(SpellChecker.class).to(SpellCheckerImpl.class);
}
}
interface SpellChecker {
public double getId();
public void checkSpelling();
}
class SpellCheckerImpl implements SpellChecker {
double id;
public SpellCheckerImpl(){
id = Math.random();
}
@Override
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
@Override
public double getId() {
return id;
}
}
Output
Compile and run the file, you may see the following output with different numbers −
0.8917278158910874 0.7214864684379797