2016-01-20 13 views
5

Qualsiasi esempio di iniezioni statiche di Dagger 2. Ho già provato questo: -Daga 2 Iniezioni statiche

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

Perché avete bisogno di essere statica? Un fornitore singleton dovrebbe ottenere ciò di cui hai bisogno. – davehenry

+0

@davehenry Si potrebbe desiderare che sia statico in modo che possa essere utilizzato con un metodo statico. –

risposta

1

Quindi questa è la mia risposta proposto: -

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

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

public interface AppComponent{ 
B getB(); 
} 
Problemi correlati