java [Tom&Jerry-Dagger2] #tags:Android

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java [Tom&Jerry-Dagger2] #tags:Android相关的知识,希望对你有一定的参考价值。

/** Dagger 2 injection in non Activity Java class **/

/** You should generally use constructor injection whenever possible. The call to component.inject(myObject) is mostly to be used for objects which you can not instantiate yourself (like activities or fragments).
Constructor injection is basically what you already did: **/

private class MyManager {
    private SharedPreferencesManager manager;
    @Inject
    MyManager(SharedPreferencesManager manager){
          this.manager = manager;           
    } 
}
/** 
  Dagger will create the object for you and pass in your SharedPreferencesManager
  There is no need to call init or something similar.
**/
/** The real question is how to obtain an object of MyManager. To do so, again, dagger will handle it for you.**/
/** By annotating the constructor with @Inject you tell dagger how it can create an object of that type. To use it, just inject it or declare it as a dependency.**/

private class MyActivity extends Activity {
    @Inject
    MyManager manager;

    public void onCreate(Bundle savedState){
        component.inject(this);  //
    } 
}

/** Or just add a getter to a component (as long as SharedPreferenceManager can be provided, MyManager can also be instantiated):**/
@Component(dependencies = SharedPreferenceManagerProvidingComponent.class)
public interface MyComponent {
    MyManager getMyManager();
}
/** How to inject pojo dependencies using dagger 2? List or Primitive Type Dependency**/
/** Below is the problem **/

public class MySimpleClass {
    private List<String> mDependency;
    public MySimpleClass (List<String> dependency) {
        mDependency = dependency;
    }
}

/**And I'm trying to have it created using dependency injection using dagger 2. Right now I have a simple module and component for it:**/
@Module
public class MySimpleClassModule {
    @Provides
    MySimpleClass provideMySimpleClass(List<String> dependency) {
        return new MySimpleClass(dependency);
    }
}

@Component(modules={MySimpleClassModule.class})
public interface MySimpleClassComponent {
}

/**But I'm not sure how can I inject the List<String> dependency every time I need to create a new instance of MySimpleClass. In the above scenario, it seems I would actually need to add List<String> to the constructor of MySimpleClassModule and have a new instance of that module every time I need a new instance of MySimpleClass with a new List<String>. Is that correct? It seems like a lot of overhead in this particular case.**/

/*****
Solution:
********/
/** No, it is not.**/
/** I assume you got a compilation error with Dagger since from question it is not clear if you already have a module that provides this list of strings.
To fix that one you can simply:
**/
@Module
public class MySimpleClassModule {

    @Provides
    List<String> provideListDependency() {
        return Arrays.asList("One", "Two");
    }

    @Provides
    MySimpleClass provideMySimpleClass(List<String> dependency) { ///<--- This list dependency come from above method
        return new MySimpleClass(dependency);
    }
}
/**If you think that providing this list should be part of a different module you can move it. The main thing, that Dagger during compilation was able to find how to get this dependency.**/


以上是关于java [Tom&Jerry-Dagger2] #tags:Android的主要内容,如果未能解决你的问题,请参考以下文章

HDOJ 1507 Uncle Tom&#39;s Inherited Land*

Nginx+Keepalived+Tomcat+MySQL负载均衡& 通过nginx调度器访问Tom

Dict & Set usage

数组中去除重复的对象的简单方法and&&的使用

从键盘输入某同学的英文名(小写输入,假设学生的英文名只包含3个字母。如: tom),编写程序在屏幕上输出该同学的英文名,且首字母大写(如: Tom)。同时输出组成该英文名的所有英文字符在26个英文字母

从键盘输入某同学的英文名(小写输入,假设学生的英文名只包含3个字母。如: tom),编写程序在屏幕上输出该同学的英文名,且首字母大写(如: Tom)。同时输出组成该英文名的所有英文字符在26个英文字母