【问题标题】:Dagger 2 Static InjectionsDagger 2 静态注入
【发布时间】:2016-01-20 15:14:48
【问题描述】:

Dagger 2 静态注入的任何示例。我已经尝试过了:-

class A{
 @Inject
 static B b;

 static {
  getAppInstance().getComponent().inject(A.class);
 }

 static anyMethod(){
  b.anotherMethod();  
 }
}

public interface AppComponent{
 void inject(Class<A> aClass);
}

【问题讨论】:

  • 为什么需要它是静态的?单例提供者应该可以满足您的需求。
  • @davehenry 您可能希望它是静态的,以便可以与静态方法一起使用。

标签: android dependency-injection static dagger-2


【解决方案1】:

所以这是我提出的答案:-

class A{
 private static B b = getAppInstance.getComponent().getB();

 static anyMethod(){
  b.anotherMethod();  
 }
}

public interface AppComponent{
 B getB();
}

【讨论】:

    猜你喜欢
    • 2014-08-30
    • 2022-09-30
    • 2018-06-13
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多