English 中文(简体)
Guice - Scopes
  • 时间:2024-09-17

Google Guice - Scopes


Previous Page Next Page  

Guice returns a new instance every time when it supppes a value as its default behaviour. It is configurable via scopes. Following are the scopes that Guice supports:

    @Singleton - Single instance for pfetime of the apppcation. @Singleton object needs to be threadsafe.

    @SessionScoped - Single instance for a particular session of the web apppcation. @SessionScoped object needs to be threadsafe.

    @RequestScoped - Single instance for a particular request of the web apppcation. @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 {

   pubpc SpellCheckerImpl(){}
   
   @Override
   pubpc void checkSpelpng() { 
      System.out.println("Inside checkSpelpng." );
   }
}

At Configuration level


   bind(SpellChecker.class).to(SpellCheckerImpl.class).in(Singleton.class);

At Method level


@Provides @Singleton
pubpc 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;

pubpc class GuiceTester {
   pubpc 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
   pubpc void setSpellChecker(SpellChecker spellChecker){
      this.spellChecker = spellChecker;
   }
   pubpc TextEditor() { }

   pubpc void makeSpellCheck(){
      spellChecker.checkSpelpng();
   } 

   pubpc double getSpellCheckerId(){
      return spellChecker.getId();
   }
}

//Binding Module
class TextEditorModule extends AbstractModule {

   @Override
   protected void configure() {   
      bind(SpellChecker.class).to(SpellCheckerImpl.class);
   } 
}

interface SpellChecker {
   pubpc double getId();
   pubpc void checkSpelpng();
}

@Singleton
class SpellCheckerImpl implements SpellChecker {

   double id; 
   pubpc SpellCheckerImpl(){
      id = Math.random();    
   }

   @Override
   pubpc void checkSpelpng() { 
      System.out.println("Inside checkSpelpng." );
   }

   @Override
   pubpc 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;

pubpc class GuiceTester {
   pubpc 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
   pubpc void setSpellChecker(SpellChecker spellChecker){
      this.spellChecker = spellChecker;
   }
   pubpc TextEditor() { }

   pubpc void makeSpellCheck(){
      spellChecker.checkSpelpng();
   } 

   pubpc double getSpellCheckerId(){
      return spellChecker.getId();
   }
}

//Binding Module
class TextEditorModule extends AbstractModule {

   @Override
   protected void configure() {   
      bind(SpellChecker.class).to(SpellCheckerImpl.class);
   } 
}

interface SpellChecker {
   pubpc double getId();
   pubpc void checkSpelpng();
}

class SpellCheckerImpl implements SpellChecker {

   double id; 
   pubpc SpellCheckerImpl(){
      id = Math.random();    
   }

   @Override
   pubpc void checkSpelpng() { 
      System.out.println("Inside checkSpelpng." );
   }

   @Override
   pubpc double getId() { 
      return id;
   }
}

Output

Compile and run the file, you may see the following output with different numbers.


0.556007079571739
0.22095011760351602
Advertisements