Created
August 20, 2019 14:56
-
-
Save amitkma/951d6f8453eb092eaa4bfd86de524ef6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
public class BookFinderModule { | |
@Provides | |
BookFinder provideBookFinder() { | |
return new OxfordBookFinder(); | |
} | |
@Provides | |
@LocalFinder | |
BookFinder provideLocalBookFinder() { | |
return new LocalLibraryBookFinder(); | |
} | |
} | |
@Component(modules = {BookFinderModule.class}) | |
public interface CommonComponent{ | |
void inject(Library library); | |
} | |
class Library { | |
private BookFinder finder; | |
@Inject | |
@LocalFinder // Qualifier | |
private BookFinder localBookFinder | |
public static void main(String[] args) { | |
CommonComponent component = DaggerComponent.builder().build(); | |
component.inject(this); | |
// Both finder and localBookFinder gets initialized | |
// just before it and are ready to use. | |
} | |
@Inject | |
public void setBookFinder(BookFinder finder) { | |
// whatever we can do here is upto ourselves, Dagger will | |
// simply provide us with the requested dependent object | |
// here | |
this.finder = finder; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment