2016-01-20 112 views
5

匕首2静态注射的任何示例。我已经尝试过这样的: -匕首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); 
} 
+0

为什么你需要它是静态的?单身人士提供者应该达到你所需要的。 – davehenry

+0

@davehenry您可能希望它是静态的,因此可以使用静态方法。 –

回答

1

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

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

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

public interface AppComponent{ 
B getB(); 
}