如何在Roboguice中通过构造函数注入参数? [安卓] [英] How can I inject parameter through constructor in Roboguice? [android]

查看:113
本文介绍了如何在Roboguice中通过构造函数注入参数? [安卓]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题可能与该问题完全相同 使用Guice将参数传递给构造函数

This question is probably exact duplicate of this one Pass parameter to constructor with Guice

区别是我使用的是roboguice,而不是Guice,所以答案对我不起作用.

Difference is that I use roboguice for android, not just Guice, so answers there does not work for me.

问题是-如何将初始化参数传递给创建的对象? IE.我注入了应该使用roboguice不知道的参数初始化的接口.

Question is - how can I pass initialize parameters into created object? I.e. I have injected interface which should be initialize with some parameter which roboguice does not know.

我在提供的链接中看到的内容,我应该创建工厂界面并像这样注册它

What I see in link I provide, I should create factory interface and register it like this

  void configure(Binder binder) {
   binder.install(new FactoryModuleBuilder()
         .implement(FooInterface.class, Foo.class)
         .build(FooFactory.class));
  }


但是我找不到 FactoryModuleBuilder 类.我使用Intellij IDEA,它可以向我显示我可以在当前位置访问的每个类,而且我可以100%确保没有以'Factory'词开头的类.

But I can't find FactoryModuleBuilder class. I use Intellij IDEA, it can show me every class which I can access at current place and I can be 100% sure that there is no classes which starts with 'Factory' word.

如何使用roboguice创建工厂?

How can I create my factory using roboguice?

已更新

我忘记下载 guice辅助注射.但是我仍然不知道应该在哪里注册这个工厂.

I forgot to download guice-assistedinject. But still I can't figure out where should I register this factory.

更新2

我为什么需要那个?因为应该存在某些抽象具有Roboguice无法解决的依赖关系的情况.这种依赖关系可以是任何类型,甚至可以是简单的字符串或数字.

Why I need that? Because there should be situation where some abstraction has dependency which could not be resolved by Roboguice. This dependency could be any type, even simple string or number.

就我而言,我在UI上具有NumberPicker控件,我想将所有特定于UI的任务移至 MyNumberPickerWrapper 类中.当我创建这个包装器时,我通过构造函数注入了它的依赖项(此控件).

In my case I have NumberPicker control on UI and I want to move all UI specific tasks in MyNumberPickerWrapper class. And when I create this wrapper I inject its dependency (this control) through constructor.

我对这种方法是否正确并不重要,但可能还有很多其他更适用的示例,其中需要构造函数注入,而Roboguice无法创建此注入的类

It's not the point if I am right with such approach, but that there could be a plenty of another more applicable example where constructor injection needed and this injected classes could not be created by Roboguice

推荐答案

我按照

I followed the steps of the answer given in Pass parameter to constructor with Guice and did slight modifications to run it under roboguice. Works completely fine for me.

  1. guice-assistinject 库添加到gradle脚本

  1. add guice-assistinject library to gradle script

dependencies { compile 'com.google.inject.extensions:guice-assistedinject:4.+' }

  • 创建带有创建方法的工厂接口,该方法接受对象构造函数所需的参数并返回对象的接口

  • Create Factory interface that with create method that accepts parameters the object constructor requires and returns object's interface

    public interface ICustomObjectFactory {
        ICustomObject create(String queueName);
    } 
    

  • 在对象的构造函数中添加 @Inject 注释,并在工厂中的每个参数中添加 @Assisted 注释.

  • Add @Inject annotation to constructor of the object and @Assisted annotation to each parameter coming from factory.

    public class CustomObject implements ICustomObject {
        protected String name;
    
        @Inject
        public CustomObject(@Assisted String name){
           this.name = name;
        }
    }
    

  • 将绑定添加到您正在使用的模块中

  • Add binding into the Module that you are using

    public class SomeModule extends AbstractModule {
    
        @Override
        protected void configure() {
             install(new FactoryModuleBuilder()
                .implement(ICustomObject.class, CustomObject.class)
                .build(ICustomObjectFactory.class));
        }
    }
    

  • 注入工厂并创建对象的实例

  • Inject factory and create instances of your object

    public class SomeClass {
    
        @Inject ICustomObjectFactory factory;
    
        public SomeClass () {
            ICustomObject first = this.factory.create("first");
            ICustomObject second = this.factory.create("second");
        }
    }
    

  • 这篇关于如何在Roboguice中通过构造函数注入参数? [安卓]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆