roboguice如何注入自定义类 [英] roboguice how to inject custom class

查看:235
本文介绍了roboguice如何注入自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我现在使用roboguice 因为我们知道,我们可以使用注解获得一流注入 如

  @InjectView(R.id.list)的ListView x
 

在@注入符号的作品,因为我从RoboActivity延长,或者任何机器人类

我的问题是 如果我想注入的自定义类,名为

 公共类CustomUtilManager {
}
 

我希望能够注入它说RoboActivity

  @注入CustomUtilMananger
 

我怎么办呢?

我的第二个问题是,如果我有一个类,不是任何机器人的子类*类

 公共类个MyOwnClass {
}
 

我如何得到的注射器和注射另一种注射类,如CustomUtilManager

即。我怎么能说

 公共类个MyOwnClass {
    @注入CustomUtilManager customUtilManager;
}
 

解决方案

注射RoboActivity的定制类

您可以简单地使用 @Inject 标注注入一个自定义类,但注入的类必须满足的一个的下列条件:

  • 在自定义类有一个默认的构造函数(不带参数)
  • 在自定义类有一个注入的构造。
  • 在自定义类有一个<一个href="https://github.com/roboguice/roboguice/wiki/CustomBinding#optional-using-a-provider-for-the-dependency">Provider该处理实例(更复杂的)

最简单的方法显然是使用默认的构造函数。 如果必须的参数在构造函数,它必须被注入:

 公共类CustomClass {

    @注入
    公共CustomClass(上下文的背景下,其他等){
        ...
    }

}
 

注意在构造函数中的 @Inject 注释。类中的每个参数也必须是注射用RoboGuice。由RoboGuice提供开箱即用几针为Android类(例如上下文)。通过RoboGuice 提供注射

注射的的自定义类

如果您创建RoboGuice自定义类的实例(例如与 @Inject 注释),所有标有田 @注入将自动注入。

如果你想使用新CustomClass(),你必须自己做注射:

 公共类CustomClass {

    @注入
    其他其他;

    富富;

    公共CustomClass(上下文的背景下){
        最后RoboInjector注射器= RoboGuice.getInjector(上下文);

        //这将注入标有@Inject标注所有领域
        injector.injectMembersWithoutViews(本);

        //这将创建富实例
        富= injector.getInstance(让Foo.class);
    }

}
 

请注意,您必须通过上下文来的构造能够得到喷油器。

hi i am currently using roboguice as we know, we can use annotation to get class injected such as

@InjectView(R.id.list)ListView x

the @inject notation works, because i extend from RoboActivity, or any Robo class

my question is if i want to inject a custom class, called

public class CustomUtilManager {
}

i want to be able to Inject it in say RoboActivity

@Inject CustomUtilMananger

how do i do it?

my second question is, if i have a class, that is not subclass of any Robo* class

say

public class MyOwnClass {
}

how do i get the injector and inject another injectable class, such as CustomUtilManager

i.e. how can i say

public class MyOwnClass {
    @Inject CustomUtilManager customUtilManager;
}

解决方案

Injection of a custom class in RoboActivity

You can inject a custom class simply using the @Inject annotation, but the injected class must satisfy one of the following conditions:

The easiest way is obviously to use a default constructor. If you must have arguments in your constructor, it must be injected :

public class CustomClass {

    @Inject
    public CustomClass(Context context, Other other) {
        ...
    }

}

Notice the @Inject annotation on the constructor. The class of each argument must also be injectable by RoboGuice. Several injections for Android classes are provided out of the box by RoboGuice (for example Context). Injections provided by RoboGuice

Injection inside a custom class

If you create the instance of your custom class with RoboGuice (for example with the @Inject annotation), all the fields marked with @Inject will be injected automatically.

If you want to use new CustomClass(), you'll have to do the injection yourself:

public class CustomClass {

    @Inject
    Other other;

    Foo foo;

    public CustomClass(Context context) {
        final RoboInjector injector = RoboGuice.getInjector(context);

        // This will inject all fields marked with the @Inject annotation
        injector.injectMembersWithoutViews(this);

        // This will create an instance of Foo
        foo = injector.getInstance(Foo.class);
    }

}

Note that you have to pass a Context to your constructor to be able to get the injector.

这篇关于roboguice如何注入自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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