Android 匕首2静态注射
匕首2静态注射的任何示例。我已经试过了:-Android 匕首2静态注射,android,dependency-injection,static,dagger-2,Android,Dependency Injection,Static,Dagger 2,匕首2静态注射的任何示例。我已经试过了:- class A{ @Inject static B b; static { getAppInstance().getComponent().inject(A.class); } static anyMethod(){ b.anotherMethod(); } } 公共接口AppComponent{ 无效注入(类aClass); } 所以这是我建议的答案:- class A{ private static B b = g
class A{
@Inject
static B b;
static {
getAppInstance().getComponent().inject(A.class);
}
static anyMethod(){
b.anotherMethod();
}
}
公共接口AppComponent{
无效注入(类aClass);
}
所以这是我建议的答案:-
class A{
private static B b = getAppInstance.getComponent().getB();
static anyMethod(){
b.anotherMethod();
}
}
为什么你需要它是静态的?单例提供程序应该满足您的需要。@davehenry您可能希望它是静态的,以便可以与静态方法一起使用。
class A{
private static B b = getAppInstance.getComponent().getB();
static anyMethod(){
b.anotherMethod();
}
}
public interface AppComponent{
B getB();
}