2015-05-23 16 views
9

To co obecnie mam i to działa:komponent Dagger2 z więcej niż jednej zależności

@FragmentScope 
@Component(dependencies = {FacebookComponent.class}, 
      modules = {FragmentFacebookLoginModule.class}) 
public interface FragmentFacebookLoginComponent { 

    void inject(FragmentFacebookLogin fragment); 
} 

Teraz chcę dodać kolejną zależność. Zmieniłem go w ten sposób:

@Component(dependencies = {FacebookComponent.class, AnotherComponent.class}, 
      modules = {FragmentFacebookLoginModule.class}) 

Ale teraz otrzymuję komunikat o błędzie:

FragmentFacebookLoginComponent zależy więcej niż jeden składnik scoped

Jak mogę rozwiązać ten problem? Jak mogę mieć więcej niż jedną zależność?

Jeśli usunąć zakres od jednego komponentu I ten komunikat o błędzie:

AnotherComponent (unscoped) nie może zależeć od scoped komponentów

Odpowiedz

4

znalazłem odpowiedź tutaj: https://stackoverflow.com/a/29619594/1016472

Na końcu stworzyłem AppComponent z odpowiednim zakresem i pozwolę FacebookComponent i AnotherComponent rozszerzyć ten AppComponent.

FacebookComponent i AnotherComponent nie mają własnego zakresu (usunąłem go).

Wygląda teraz tak:

@AppScope 
@Component 
public interface AppComponent { 

} 


@Component(modules = {FacebookModule.class}) 
public interface FacebookComponent extends AppComponent { 

} 


@Component(modules = {AnotherModule.class}) 
public interface AnotherComponent extends AppComponent { 

} 


@FragmentScope 
@Component(dependencies = {FacebookComponent.class, AnotherComponent.class}, 
      modules = {FragmentFacebookLoginModule.class}) 
public interface FragmentFacebookLoginComponent { 

    void inject(FragmentFacebookLogin fragment); 
} 
+0

It ** działa naprawdę **? Wydaje mi się to trochę dziwne. FacebookModule i AnotherModule udostępnia obiekty o zasięgu @AppScope? – afj88

+0

@ afj88 Nie wiem, czy zadziała, ale zadziałało 2 lata temu –

+3

potwierdzone, nie działa już dla 'dagger-2.10' – Leo

2

Co chcesz być uznane za w ApplicationScope powinny być wszystkie zdefiniowane bez zakresie, a połączone ze sobą w zakres stosowania tylko w ApplicationComponent pod danym zakresie .

Na przykład

@Component(modules = {FacebookModule.class}) 
public interface FacebookComponent { 
    FacebookThing facebookThing(); //assuming this is with @Provides in FacebookModule with NO SCOPE 
} 


@Component(modules = {AnotherModule.class}) 
public interface AnotherComponent{ 
    AnotherThing anotherThing(); //assuming this is with @Provides in AnotherModule with NO SCOPE 
} 

Następnie można zrobić

@AppScope 
@Component(dependencies={AnotherComponent.class, FacebookComponent.class}) 
public interface AppComponent extends AnotherComponent, FacebookComponent {} 

Po czym można zrobić

@FragmentScope 
@Component(dependencies=AppComponent.class) 
public interface FragmentComponent extends AppComponent {} 

Uwaga unscoped dostawcy utworzyć nową instancję na każde wezwanie injekcji . Jeśli chcesz uzyskać zasięg, powinieneś związać moduły z tym samym komponentem, ale komponenty powinny tylko zależeć od innych komponentów w celu podcięcia.

0

Dołącz na module moduł zależność takiego:

@Module(includes = FacebookModule.class) 
public class AnotherModule {... 
Powiązane problemy