Google Guice Tutorial on Google Guice Scopes

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;
}

example

let's see the scope at class level in action.

result with @singleton annotation

create a java class named guicetester.

guicetester.java

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;
   }
}

compile and run the file, you may see the following output with same numbers.

0.3055839187063575
0.3055839187063575

result without @singleton annotation

create a java class named guicetester.

guicetester.java

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.556007079571739
0.22095011760351602