当前位置: 首页 > 教程 > Google Guice >

Google Guice @Provides

精华
小牛编辑
87浏览
2023-03-14

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

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

Google Guice @Provides 完整示例

创建一个名为 GuiceTester 的 Java 类。

GuiceTester.java

package cn.xnip;

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

输出

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

最后更新:

类似资料

  • 我没有问题绑定单例实例到其他一些实例使用@注入。但是,当涉及到绑定一个类的更多实例到其他一些实例(这是单例),它拒绝以某种方式绑定它,我得到了空引用。 我的代码示例是: 当我创建几个WebSocketManagerImpl实例时,每个factoryImpl字段都是空指针。我做错什么了吗?

  • 在我的Play(Java)框架项目中,我正在使用Guice进行依赖项注入,并且正在努力理解如何最好地将“会话”的概念与Guice和Play结合使用? 我知道Play是无状态的,除了可以在cookie中存储值之外,实际上没有会话的概念。我对Guice和Play的理解是,虽然Guice文档描述了支持不同的作用域(单例、会话、请求、无作用域),但因为我们正在用每个请求实例化一个新的注入器,所以仅适用于P

  • 问题内容: 我正在尝试在我的项目中实现dagger2,但遇到错误“ android.app.Application必须在没有@Inject构造函数或@Provides注释方法的情况下才能提供 ”。 这是我的代码: App.java di / AppModule.java di / AppComponent.java di / TestClassModule.java di / TestClassC

  • 问题内容: 我正在做一个很大的项目,有很多注入。当前,我们正在使用一个类,该类为需要一次的每次注入实现,并且它们大多具有一个行方法。 每当我需要一个新的提供程序时,创建一个新的类就变得很烦人。使用提供程序类比使用方法有什么好处,反之亦然? 问题答案: 据我所知,它们在大多数简单情况下是完全等效的。 无论哪种样式,即使键绑定到类或实例,Guice都可以让您注入和。如果直接获取实例,Guice会自动调

  • 我想在我的片段(HomeFragment)中注入一个依赖项(HomeViewModel)。 我有一个类(HomeViewModelImpl)实现了该抽象(HomeViewModel),在这个类中,我当然覆盖了父级的方法。 抽象类(HomeViewModel)是从BaseViewModel扩展而来的抽象类。 BaseViewModel是一个普通的开放类,它从Android生命周期组件的ViewMod

  • Guice提供了一种使用@provides方法创建与复杂对象的绑定的方法。 @Provides public SpellChecker provideSpellChecker() { String dbUrl = "jdbc:mysql://localhost:5326/emp"; String user = "user"; int timeout = 100; Spell

相关阅读

开发工具