目录

Guice - 快速指南

Google Guice - Overview

Guice是一个基于Java的开源依赖注入框架。 它非常轻巧,由Google积极开发/管理。

依赖注入

每个基于Java的应用程序都有一些对象可以协同工作,以呈现最终用户所看到的工作应用程序。 在编写复杂的Java应用程序时,应用程序类应尽可能独立于其他Java类,以增加重用这些类的可能性,并在单元测试时独立于其他类测试它们。 依赖注入(或称为布线)有助于将这些类粘合在一起,同时保持它们独立。

假设您有一个具有文本编辑器组件的应用程序,并且您想要提供拼写检查。 您的标准代码看起来像这样 -

public class TextEditor {
   private SpellChecker spellChecker;
   public TextEditor() {
      spellChecker = new SpellChecker();
   }
}

我们在这里做的是,在TextEditor和SpellChecker之间创建依赖关系。 在控制场景的反转中,我们会做这样的事情 -

public class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor(SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
}

在这里,TextEditor不应该担心SpellChecker的实现。 SpellChecker将独立实现,并在TextEditor实例化时提供给TextEditor。

使用Guice进行依赖注入(绑定)

依赖注入由Guice Bindings控制。 Guice使用绑定​​将对象类型映射到它们的实际实现。 这些绑定定义为一个模块。 模块是绑定的集合,如下所示 -

public class TextEditorModule extends AbstractModule {
   @Override 
   protected void configure() {
      /*
         * Bind SpellChecker binding to WinWordSpellChecker implementation 
         * whenever spellChecker dependency is used.
      */
      bind(SpellChecker.class).to(WinWordSpellChecker.class);
   }
}

Module是Injector的核心构建块,它是Guice的对象图构建器。 第一步是创建一个注入器,然后我们可以使用注入器来获取对象。

public static void main(String[] args) {
   /*
      * Guice.createInjector() takes Modules, and returns a new Injector
      * instance. This method is to be called once during application startup.
   */
   Injector injector = Guice.createInjector(new TextEditorModule());
   /*
      * Build object using injector
   */
   TextEditor textEditor = injector.getInstance(TextEditor.class);   
}

在上面的示例中,TextEditor类对象图由Guice构造,该图包含TextEditor对象及其作为WinWordSpellChecker对象的依赖关系。

Google Guice - Environment Setup

本地环境设置 (Local Environment Setup)

如果您仍然愿意为Java编程语言设置环境,那么本节将指导您如何在计算机上下载和设置Java。 请按照下面提到的步骤设置环境。

Java SE可从链接Download Java免费获得。 因此,您下载基于您的操作系统的版本。

按照说明下载Java并运行.exe以在您的计算机上安装Java。 在计算机上安装Java后,需要设置环境变量以指向正确的安装目录 -

设置Windows 2000/XP的路径

我们假设您已在c:\Program Files\java\jdk目录中安装了Java -

  • 右键单击“我的电脑”,然后选择“属性”。

  • 单击“高级”选项卡下的“环境变量”按钮。

  • 现在,更改'Path'变量,使其也包含Java可执行文件的路径。 例如,如果路径当前设置为“C:\WINDOWS\SYSTEM32”,则将路径更改为“C:\WINDOWS\SYSTEM32; c:\Program Files\java\jdk\bin”。

设置Windows 95/98/ME的路径

我们假设您已在c:\Program Files\java\jdk目录中安装了Java -

  • 编辑'C:\autoexec.bat'文件并在末尾添加以下行 - 'SET PATH =%PATH%; C:\Program Files\java\jdk\bin'。

设置Linux,UNIX,Solaris,FreeBSD的路径

应将环境变量PATH设置为指向已安装Java二进制文件的位置。 如果您在执行此操作时遇到问题,请参阅您的shell文档。

例如,如果使用bash作为shell,那么您可以将以下行添加到'.bashrc:export PATH =/path/to/java:$ PATH'的末尾。

流行的Java编辑器 (Popular Java Editors)

要编写Java程序,需要一个文本编辑器。 市场上有许多复杂的IDE。 但就目前而言,您可以考虑以下其中一项 -

  • Notepad - 在Windows机器上,您可以使用任何简单的文本编辑器,如记事本(本教程推荐),TextPad。

  • Netbeans - 它是一个开源和免费的Java IDE,可以从https://www.netbeans.org/index.html下载。

  • Eclipse - 它也是由eclipse开源社区开发的Java IDE,可以从https://www.eclipse.org/下载。

Google Guice环境

下载最新版本的Google Guice及相关jar文件。

在编写本教程时,我们已将它们复制到C:\> Google文件夹中。

OS 存档名称
Windowsguice-4.1.0.jar;aopalliance-1.0.jar;guava-16.0.1.jar;javax.inject-1.jar
Linuxguice-4.1.0.jar;aopalliance-1.0.jar;guava-16.0.1.jar;javax.inject-1.jar
Macguice-4.1.0.jar;aopalliance-1.0.jar;guava-16.0.1.jar;javax.inject-1.jar

设置类路径变量 (Set CLASSPATH Variable)

CLASSPATH环境变量设置为指向Guice jar位置。 假设您已将Guice及相关的jar存储在各种操作系统的Google文件夹中,如下所示。

OS output
Windows 将环境变量CLASSPATH设置为%CLASSPATH%; C:\Google\guice-4.1.0.jar; C:\Google\aopalliance-1.0.jar; C:\Google\guava-16.0.1.jar; C:\谷歌\ javax.inject-1.jar;。;
Linux export CLASSPATH = $ CLASSPATH:Google/guice-4.1.0.jar:Google/aopalliance-1.0.jar:Google/guava-16.0.1.jar:Google/javax.inject-1.jar:。
Mac export CLASSPATH = $ CLASSPATH:Google/guice-4.1.0.jar:Google/aopalliance-1.0.jar:Google/guava-16.0.1.jar:Google/javax.inject-1.jar:。

Google Guice - First Application

让我们创建一个基于控制台的示例应用程序,我们将逐步使用Guice绑定机制演示依赖注入。

第1步:创建界面

//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}

第2步:创建实施

//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   @Override
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   } 
}

第3步:创建绑定模块

//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      bind(SpellChecker.class).to(SpellCheckerImpl.class);
   } 
}

第4步:创建具有依赖性的类

class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor(SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   }
}

第5步:创建注射器

Injector injector = Guice.createInjector(new TextEditorModule());

步骤6:获取具有依赖性的对象

TextEditor editor = injector.getInstance(TextEditor.class);

第7步:使用该对象

editor.makeSpellCheck(); 

完整的示例 (Complete Example)

创建一个名为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());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck(); 
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor(SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   }
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      bind(SpellChecker.class).to(SpellCheckerImpl.class);
   } 
}
//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   @Override
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   } 
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside checkSpelling.

Google Guice - Linked Binding

在链接绑定中,Guice将类型映射到其实现。 在下面的示例中,我们将SpellChecker接口与其实现SpellCheckerImpl进行了映射。

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

我们还可以将具体类映射到它的子类。 见下面的例子 -

bind(SpellCheckerImpl.class).to(WinWordSpellCheckerImpl.class);

在这里,我们链接了绑定。 让我们看看完整示例中的结果。

完整的示例 (Complete Example)

创建一个名为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());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck(); 
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor(SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   }
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      bind(SpellChecker.class).to(SpellCheckerImpl.class);
      bind(SpellCheckerImpl.class).to(WinWordSpellCheckerImpl.class);
   } 
}
//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   @Override
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   } 
}
//subclass of SpellCheckerImpl
class WinWordSpellCheckerImpl extends SpellCheckerImpl {
   @Override
   public void checkSpelling() {
      System.out.println("Inside WinWordSpellCheckerImpl.checkSpelling." );
   } 
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside WinWordSpellCheckerImpl.checkSpelling.

Google Guice - Binding Annotations

因为我们可以用它的实现绑定一个类型。 如果我们想要映射具有多个实现的类型,我们也可以创建自定义注释。 请参阅以下示例以了解该概念。

创建绑定注释

@BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
@interface WinWord {}
  • @BindingAnnotation - 将注释标记为绑定注释。

  • @Target - 标记注释的适用性。

  • @Retention - 将注释的可用性标记为运行时。

使用绑定注释进行映射

bind(SpellChecker.class).annotatedWith(WinWord.class).to(WinWordSpellCheckerImpl.class);

使用绑定注释进行注入

@Inject
public TextEditor(@WinWord SpellChecker spellChecker) {
   this.spellChecker = spellChecker;
}

完整的示例 (Complete Example)

创建一个名为GuiceTester的java类。

GuiceTester.java

import java.lang.annotation.Target;
import com.google.inject.AbstractModule;
import com.google.inject.BindingAnnotation;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
@BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
@interface WinWord {}
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck();     
   } 
}
class TextEditor {
   private SpellChecker spellChecker;   
   @Inject
   public TextEditor(@WinWord SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling(); 
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      bind(SpellChecker.class).annotatedWith(WinWord.class)
         .to(WinWordSpellCheckerImpl.class);    
   } 
}
//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   @Override
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   } 
}
//subclass of SpellCheckerImpl
class WinWordSpellCheckerImpl extends SpellCheckerImpl {
   @Override
   public void checkSpelling() {
      System.out.println("Inside WinWordSpellCheckerImpl.checkSpelling." );
   } 
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside WinWordSpellCheckerImpl.checkSpelling.

Google Guice - @Named Binding

Guice还提供了另一种映射绑定的方法,而无需创建自定义注释。 它允许使用@Named注释。

使用命名注释进行映射

bind(SpellChecker.class).annotatedWith(Names.named("OpenOffice")).to(OpenOfficeWordSpellCheckerImpl.class);

使用@Named注释进行注入

@Inject
public TextEditor(@Named("OpenOffice") SpellChecker spellChecker) {
   this.spellChecker = spellChecker;
}

完整的示例 (Complete Example)

创建一个名为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.name.Named;
import com.google.inject.name.Names;
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck();
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor(@Named("OpenOffice") SpellChecker spellChecker) {
      this.spellChecker = spellChecker;      
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling(); 
   }  
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      bind(SpellChecker.class).annotatedWith(Names.named("OpenOffice"))
         .to(OpenOfficeWordSpellCheckerImpl.class);
   } 
}
//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   @Override
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   } 
}
//subclass of SpellCheckerImpl
class OpenOfficeWordSpellCheckerImpl extends SpellCheckerImpl {
   @Override
   public void checkSpelling() {
      System.out.println("Inside OpenOfficeWordSpellCheckerImpl.checkSpelling." );
   } 
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside OpenOfficeWordSpellCheckerImpl.checkSpelling.

Google Guice - Constant Bindings

Guice提供了一种使用值对象或常量创建绑定的方法。 考虑我们要配置JDBC URL的情况。

使用@Named注释进行注入

@Inject
public void connectDatabase(@Named("JBDC") String dbUrl) {
   //...
}

这可以使用toInstance()方法获得。

bind(String.class).annotatedWith(Names.named("JBDC")).toInstance("jdbc:mysql://localhost:5326/emp");

完整的示例 (Complete Example)

创建一个名为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.name.Named;
import com.google.inject.name.Names;
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeConnection();
   } 
}
class TextEditor {
   private String dbUrl;
   @Inject
   public TextEditor(@Named("JDBC") String dbUrl) {
      this.dbUrl = dbUrl;
   }
   public void makeConnection() {
      System.out.println(dbUrl);
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      bind(String.class)
         .annotatedWith(Names.named("JDBC"))
         .toInstance("jdbc:mysql://localhost:5326/emp");
   } 
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

jdbc:mysql://localhost:5326/emp

Google Guice - @Provides Annotation

Guice提供了一种使用@provides方法创建与复杂对象的绑定的方法。

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

此方法是绑定模块的一部分,并提供要映射的复杂对象。 请参阅下面的完整示例。

完整的示例 (Complete Example)

创建一个名为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.Provides;
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck();
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor( SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {} 
   @Provides
   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;
   }
}
//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   private String dbUrl;
   private String user;
   private Integer timeout;
   @Inject
   public SpellCheckerImpl(String dbUrl, 
      String user, 
      Integer timeout) {
      this.dbUrl = dbUrl;
      this.user = user;
      this.timeout = timeout;
   } 
   @Override
   public void checkSpelling() { 
      System.out.println("Inside checkSpelling." );
      System.out.println(dbUrl);
      System.out.println(user);
      System.out.println(timeout);
   }
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside checkSpelling.
jdbc:mysql://localhost:5326/emp
user
100

Google Guice - Provider Class

随着@provides方法变得更加复杂,可以使用Provider接口将这些方法移动到单独的类中。

class SpellCheckerProvider implements Provider<SpellChecker> {
   @Override
   public SpellChecker get() {
      String dbUrl = "jdbc:mysql://localhost:5326/emp";
      String user = "user";
      int timeout = 100;
      SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout);
      return SpellChecker;
   } 
}

下一步是将提供程序映射到type。

bind(SpellChecker.class).toProvider(SpellCheckerProvider.class);

请参阅下面的完整示例。

完整的示例 (Complete Example)

创建一个名为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.Provider;
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck();
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor( SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      bind(SpellChecker.class).toProvider(SpellCheckerProvider.class);
   } 
}
//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   private String dbUrl;
   private String user;
   private Integer timeout;
   @Inject
   public SpellCheckerImpl(String dbUrl, 
      String user, 
      Integer timeout) {
      this.dbUrl = dbUrl;
      this.user = user;
      this.timeout = timeout;
   } 
   @Override
   public void checkSpelling() { 
      System.out.println("Inside checkSpelling." );
      System.out.println(dbUrl);
      System.out.println(user);
      System.out.println(timeout);
   }
}
class SpellCheckerProvider implements Provider<SpellChecker> {
   @Override
   public SpellChecker get() {
      String dbUrl = "jdbc:mysql://localhost:5326/emp";
      String user = "user";
      int timeout = 100;
      SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout);
      return SpellChecker;
   }
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside checkSpelling.
jdbc:mysql://localhost:5326/emp
user
100

Google Guice - Constructor Bindings

Guice提供了一种使用toConstructor()方法与对象的特定构造函数创建绑定的方法。

@Override
protected void configure() {
   try {
      bind(SpellChecker.class)
         .toConstructor(SpellCheckerImpl.class.getConstructor(String.class));
   } catch (NoSuchMethodException | SecurityException e) {
      System.out.println("Required constructor missing");
   } 
} 

请参阅下面的完整示例。

完整的示例 (Complete Example)

创建一个名为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.name.Named;
import com.google.inject.name.Names;
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck();
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor( SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      try {
         bind(SpellChecker.class)
            .toConstructor(SpellCheckerImpl.class.getConstructor(String.class));
      } catch (NoSuchMethodException | SecurityException e) {
         System.out.println("Required constructor missing");
      } 
      bind(String.class)
         .annotatedWith(Names.named("JDBC"))
         .toInstance("jdbc:mysql://localhost:5326/emp");
   } 
}
//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   private String dbUrl;
   public SpellCheckerImpl(){}
   public SpellCheckerImpl(@Named("JDBC") String dbUrl) {
      this.dbUrl = dbUrl;
   } 
   @Override
   public void checkSpelling() { 
      System.out.println("Inside checkSpelling." );
      System.out.println(dbUrl); 
   }
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside checkSpelling.
jdbc:mysql://localhost:5326/emp

Google Guice - Inbuilt Bindings

Guice为java.util.logging.Logger类提供内置绑定。 记录器的名称自动设置为注入Logger的类的名称。 请参阅下面的示例。

例子 (Example)

创建一个名为GuiceTester的java类。

GuiceTester.java

import java.util.logging.Logger;
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());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck();
   } 
}
class TextEditor {
   private Logger logger;
   @Inject
   public TextEditor( Logger logger) {
      this.logger = logger;
   }
   public void makeSpellCheck() {
      logger.info("In TextEditor.makeSpellCheck() method");
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
   } 
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Dec 20, 2017 12:51:05 PM TextEditor makeSpellCheck
INFO: In TextEditor.makeSpellCheck() method

Google Guice - Just-in-time Bindings

由于绑定是在绑定模块中定义的,因此只要需要注入依赖关系,Guice就会使用它们。 如果不存在绑定,它可以尝试创建即时绑定。 绑定模块中存在的绑定称为显式绑定,具有更高的优先级,而即时绑定称为隐式绑定。 如果存在两种类型的绑定,则考虑使用显式绑定进行映射。

以下是三种即时绑定的示例。

Sr.No. 绑定类型和描述
1 可注射的构造函数

非私有,无参数构造函数符合即时绑定的条件。 另一种方法是使用@Inject批注来注释构造函数。

2 @ImplementatedBy注释

@ImplementatedBy注释告诉guice有关实现类。 在这种情况下,绑定模块不需要绑定。

3 @ProvidedBy注释

@ProvidedBy注释告诉guice有关实现类的提供者。 在这种情况下,绑定模块不需要绑定。

Google Guice - Constructor Injection

注入是将依赖注入对象的过程。 构造函数注入很常见。 在此过程中,依赖项作为参数注入构造函数。 请参阅下面的示例。

创建一个名为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());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck(); 
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor(SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   }
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      bind(SpellChecker.class).to(SpellCheckerImpl.class);
   } 
}
//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   @Override
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   } 
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside checkSpelling.

Google Guice - Method Injection

注入是将依赖注入对象的过程。 方法注入用于将值对象设置为对象的依赖项。 请参阅下面的示例。

例子 (Example)

创建一个名为GuiceTester的java类。

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.ImplementedBy;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck();
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor( SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() { 
      bind(String.class)
         .annotatedWith(Names.named("JDBC"))
         .toInstance("jdbc:mysql://localhost:5326/emp");
   } 
}
@ImplementedBy(SpellCheckerImpl.class)
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   private String dbUrl;
   public SpellCheckerImpl(){}
   @Inject 
   public void setDbUrl(@Named("JDBC") String dbUrl) {
      this.dbUrl = dbUrl;
   }
   @Override
   public void checkSpelling() { 
      System.out.println("Inside checkSpelling." );
      System.out.println(dbUrl); 
   }
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside checkSpelling.
jdbc:mysql://localhost:5326/emp

Google Guice - Field Injection

注入是将依赖注入对象的过程。 字段注入用于将值对象设置为对象字段的依赖项。 请参阅下面的示例。

例子 (Example)

创建一个名为GuiceTester的java类。

GuiceTester.java

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.ImplementedBy;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck();
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor( SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() { 
      bind(String.class)
         .annotatedWith(Names.named("JDBC"))
         .toInstance("jdbc:mysql://localhost:5326/emp");
   } 
}
@ImplementedBy(SpellCheckerImpl.class)
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   @Inject @Named("JDBC")
   private String dbUrl;
   public SpellCheckerImpl(){}
   @Override
   public void checkSpelling() { 
      System.out.println("Inside checkSpelling." );
      System.out.println(dbUrl); 
   }
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside checkSpelling.
jdbc:mysql://localhost:5326/emp

Google Guice - Optional Injection

注入是将依赖注入对象的过程。 可选注入意味着在存在时注入依赖项。 方法和场注入可以是可选的,并且如果不存在依赖性,则应该具有一些默认值。 请参阅下面的示例。

例子 (Example)

创建一个名为GuiceTester的java类。

GuiceTester.java

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.ImplementedBy;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.name.Named;
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck();
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor( SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {} 
}
@ImplementedBy(SpellCheckerImpl.class)
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   private String dbUrl = "jdbc:mysql://localhost:5326/emp";
   public SpellCheckerImpl(){}
   @Inject(optional=true)
   public void setDbUrl(@Named("JDBC") String dbUrl) {
      this.dbUrl = dbUrl;
   }
   @Override
   public void checkSpelling() { 
      System.out.println("Inside checkSpelling." );
      System.out.println(dbUrl); 
   }
}

输出 (Output)

编译并运行该文件,您将看到以下输出。

Inside checkSpelling.
jdbc:mysql://localhost:5326/emp

Google Guice - On-Demand Injection

注入是将依赖注入对象的过程。 方法和字段注入可用于使用injector.injectMembers()方法使用现有对象进行初始化。 请参阅下面的示例。

例子 (Example)

创建一个名为GuiceTester的java类。

GuiceTester.java

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.ImplementedBy;
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);     
      editor.makeSpellCheck();
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public void setSpellChecker(SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public TextEditor() { }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   } 
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {      
   } 
}
@ImplementedBy(SpellCheckerImpl.class)
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   public SpellCheckerImpl(){}
   @Override
   public void checkSpelling() { 
      System.out.println("Inside checkSpelling." );
   }
}

编译并运行该文件,您将看到以下输出。

Inside checkSpelling.

Google Guice - Scopes

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

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

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

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

应用范围的方法

以下是应用范围的方法。

在class

@Singleton
class SpellCheckerImpl implements SpellChecker {
   public SpellCheckerImpl(){}
   @Override
   public void checkSpelling() { 
      System.out.println("Inside checkSpelling." );
   }
}

在配置级别

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

在方法级别

@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)

让我们看看课堂上的范围。

结果使用@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

没有@Singleton注释的结果

创建一个名为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;
   }
}

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

0.556007079571739
0.22095011760351602

Google Guice - AOP

AOP,面向方面的编程需要将程序逻辑分解为称为所谓的关注点的不同部分。 跨越应用程序多个点的功能称为跨领域问题,这些跨领域问题在概念上与应用程序的业务逻辑分开。 有许多常见的好例子,如日志记录,审计,声明式事务,安全性,缓存等。

OOP中模块化的关键单元是类,而在AOP中,模块化单元是方面。 依赖注入可以帮助您将应用程序对象相互分离,AOP可以帮助您将交叉问题与它们所影响的对象分离。 AOP就像Perl,.NET,Java等编程语言中的触发器。 Guice提供拦截器来拦截应用程序。 例如,执行方法时,可以在方法执行之前或之后添加额外的功能。

重要的课程

  • Matcher - Matcher是接受或拒绝值的接口。 在Guice AOP中,我们需要两个匹配器:一个用于定义哪些类参与,另一个用于这些类的方法。

  • MethodInterceptor - 在调用匹配方法时执行MethodInterceptors。 他们可以检查调用:方法,参数和接收实例。 我们可以执行横切逻辑,然后委托给底层方法。 最后,我们可以检查返回值或异常并返回。

例子 (Example)

创建一个名为GuiceTester的java类。

GuiceTester.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.matcher.Matchers;
public class GuiceTester {
   public static void main(String[] args) {
      Injector injector = Guice.createInjector(new TextEditorModule());
      TextEditor editor = injector.getInstance(TextEditor.class);
      editor.makeSpellCheck(); 
   } 
}
class TextEditor {
   private SpellChecker spellChecker;
   @Inject
   public TextEditor(SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
   public void makeSpellCheck() {
      spellChecker.checkSpelling();
   }
}
//Binding Module
class TextEditorModule extends AbstractModule {
   @Override
   protected void configure() {
      bind(SpellChecker.class).to(SpellCheckerImpl.class);
      bindInterceptor(Matchers.any(), 
         Matchers.annotatedWith(CallTracker.class), 
         new CallTrackerService());
   } 
}
//spell checker interface
interface SpellChecker {
   public void checkSpelling();
}
//spell checker implementation
class SpellCheckerImpl implements SpellChecker {
   @Override @CallTracker
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   } 
}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
@interface CallTracker {}
class CallTrackerService implements MethodInterceptor  {
   @Override
   public Object invoke(MethodInvocation invocation) throws Throwable {
      System.out.println("Before " + invocation.getMethod().getName());
      Object result = invocation.proceed();
      System.out.println("After " + invocation.getMethod().getName());
      return result;
   }
}

编译并运行该文件,您可能会看到以下输出。

Before checkSpelling
Inside checkSpelling.
After checkSpelling
<上一篇.Guice - AOP
↑回到顶部↑
WIKI教程 @2018