2016-01-20 19 views
5

Jedes Beispiel für statische Injektionen Dolch 2. Ich habe dies bereits versucht: -Dolch 2 Statische Injektionen

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

Warum brauchen Sie es statisch zu sein? Ein Singleton-Anbieter sollte erreichen, was Sie brauchen. – davehenry

+0

@davehenry Sie möchten, dass es statisch ist, damit es mit einer statischen Methode verwendet werden kann. –

Antwort

1

Also das ist meine vorgeschlagene Antwort: -

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

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

public interface AppComponent{ 
B getB(); 
}