Posted on Leave a comment

Provide different implementations of an interface

Use @Qualifier

[code language=”java”]
@Qualifier
@Documented
@Retention(RUNTIME)
public @interface Named {
/** The name. */
String value() default "";
}
[/code]

[code language=”java”]
@Inject
@Named("Database")
Storage storageDB;

@Inject
@Named("Cache")
Storage storageCache;
[/code]

[code language=”java”]
@Module
public class AppModule {

@Provides
@Singleton
@Named("Database")
public Storage provideDatabaseStorage() {
return new Database();
}

@Provides
@Singleton
@Named("Cache")
public Storage provideCacheStorage() {
return new Cache();
}
}
[/code]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.