简体   繁体   English

具有roboguice的COnstructor注入进入无限循环

[英]COnstructor Injection with roboguice enters inifinite loop

HI I come from a background C#/.NET and have learned to work with a bit of Android.Now I am wana start to build a small app for fun and I tought to learn an IoC framework.After a bit of googeling I found roboguice.But I can not figure out how to integrate it.On . 嗨,我来自C#/。NET的后台,已经学会了使用Android的工作原理,现在我想开始创建一个有趣的小应用程序,然后我就想学习一个IoC框架。但是我不知道如何集成它。

NET I have worked with Ninject and Unity , and am looking to create a similar form of constructor injection that I got from those frameworks. NET,我曾与Ninject和Unity一起工作过,并且希望创建一种类似形式的构造函数注入,这种形式是我从那些框架中获得的。

Here is what I have so far and what I think I have figured out: 到目前为止,这是我想知道的东西:

This class represent the app bootstrapper and here is where I will register my dependency config class: 此类表示应用程序引导程序,在这里我将注册我的依赖项配置类:

public class IOCApplication extends Application{

@Override
public void onCreate(){
    super.onCreate();
    RoboGuice.setBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE, RoboGuice.newDefaultRoboModule(this), new IOCModule());
}

} }

This is the Dependency config class: 这是Dependency配置类:

 public class IOCModule implements Module{

        @Override
        public void configure(com.google.inject.Binder binder) {
            binder.bind(ITest.class).to(Test.class);
        }
    }

In my AndroidManifest I have added this: 在我的AndroidManifest中,我添加了以下内容:

<application ... android:name="com.example.project2.IOCApplication">

This part I do not realy understad why I had to add but I am thinking it's something to tell Android that IOCApplication should be isntantiated first. 我并不真正理解为什么必须添加这部分,但是我认为这是在告诉Android IOCApplication应该首先被实例化。

This is the clas my MainActivily class and I have added a constructor for it: 这是我的MainActivily类,我为此添加了一个构造函数:

public ITest test;
public MainActivity(ITest test){
    this.test = test;
}

When I try to runthis on my android device it kind of looks like the app is entering an infinite loop and I do not think ITest get's instantiated. 当我尝试在我的android设备上运行this时,该应用似乎进入了无限循环,我不认为ITest会被实例化。

What am I doing wrong? 我究竟做错了什么?

One thing to know with Android is that you do not instantiate your own activities , it is the system which does. Android的一件事是您不实例化自己的活动 ,而是由系统来实例化

Because of this, you cannot use constructor injection with activities. 因此,您不能将构造函数注入与活动一起使用。 However, you can use attribute injection, which is cleaner IMO. 但是,您可以使用属性注入,这是更干净的IMO。

Extending the RoboActivity class is the easiest way to use injection with Activity . 扩展RoboActivity类是对Activity使用注入的最简单方法。 RoboGuice provides similar classes for other Android components ( RoboFragment , RoboService , etc.) RoboGuice为其他Android组件( RoboFragmentRoboService等)提供了类似的类。

public class MyActivity extends RoboActivity {

    @Inject
    ITest test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // You can now use your test instance
    }

}

All attribute with the @Inject will be instantiated after super.onCreate(savedInstanceState); 具有@Inject所有属性将在super.onCreate(savedInstanceState);之后实例化super.onCreate(savedInstanceState); is called. 叫做。


With POJO (Plain Old Java Object), you have more options: 使用POJO (普通的旧Java对象),您可以有更多选择:

Attribute injection 属性注入

public class Test {
    @Inject
    private Service1 service1;
    @Inject
    private Service2 service2;

    public Test() {
    }
}

Constructor injection 构造器注入

public class Test {

    private Service1 service1;
    private Service2 service2;

    @Inject
    public Test(Service1 service1, Service2 service2) {
        this.service1 = service1;
        this.service2 = service2;
    }
}

Note that your constructor must have the @Inject annotation if it has arguments. 请注意,如果构造函数具有参数,则必须具有@Inject批注。


You need this line <application ... android:name="com.example.project2.IOCApplication"> to tell the system that you are using an extended Application class. 您需要此行<application ... android:name="com.example.project2.IOCApplication">来告诉系统您正在使用扩展的Application类。 Without it, Android will use the base class. 没有它,Android将使用基类。

I encourage you to read the official documentation for more information. 我鼓励您阅读官方文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM