匕首2静态注射

apr*_*dia 5 static android dependency-injection dagger-2

任何Dagger 2静态注射的例子.我已经尝试过这个: -

class A{
 @Inject
 static B b;

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

 static anyMethod(){
  b.anotherMethod();  
 }
}
Run Code Online (Sandbox Code Playgroud)
public interface AppComponent{
 void inject(Class<A> aClass);
}
Run Code Online (Sandbox Code Playgroud)

apr*_*dia 3

所以这是我建议的答案:-

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

 static anyMethod(){
  b.anotherMethod();  
 }
}
Run Code Online (Sandbox Code Playgroud)
public interface AppComponent{
 B getB();
}
Run Code Online (Sandbox Code Playgroud)