Guice 简明教程

Google Guice - Scopes

Guice 会在其默认行为作为其值时,每次都返回一个新实例。它可以通过范围进行配置。以下是 Guice 支持的范围:

  1. @Singleton - 应用程序生命周期的单实例。@Singleton 对象需要是线程安全的。

  2. @SessionScoped - Web 应用程序特定会话的单实例。@SessionScoped 对象需要是线程安全的。

  3. @RequestScoped - Web 应用程序特定请求的单实例。@RequestScoped 对象不需要是线程安全的。

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

让我们在类级别中查看实际作用域。

Result With @Singleton Annotation

创建名为 GuiceTester 的 Java 类。

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

编译并运行文件,可能会看到具有相同数字的以下输出。

0.3055839187063575
0.3055839187063575

Result Without @Singleton Annotation

创建名为 GuiceTester 的 Java 类。

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

编译并运行文件,可能会看到具有不同数字的以下输出。

0.556007079571739
0.22095011760351602