CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-google-inject--guice

Lightweight dependency injection framework for Java 8 and above that eliminates factories and the use of 'new' through @Inject annotation

Pending
Overview
Eval results
Files

multibindings.mddocs/

Multibindings

Advanced binding patterns for injecting collections (Set, Map) and optional values, enabling modular composition of dependencies.

Capabilities

Multibinder Class

Binds multiple values separately to be injected as a complete Set<T>.

/**
 * An API to bind multiple values separately, only to later inject them as a
 * complete collection. Multibinder is intended for use in your application's
 * module. Instead of binding multiple values separately, Multibinder collects
 * the values and provides them as a single Set<T> binding.
 * @param <T> Type of elements in the set
 */
public class Multibinder<T> {
    /**
     * Creates a new multibinder that collects instances of type T in a Set.
     * @param binder Binder to configure
     * @param type Element type
     * @return New Multibinder instance
     */
    public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T> type);
    
    /**
     * Creates a new multibinder that collects instances of type T in a Set.
     * @param binder Binder to configure
     * @param type Element type literal
     * @return New Multibinder instance
     */
    public static <T> Multibinder<T> newSetBinder(Binder binder, TypeLiteral<T> type);
    
    /**
     * Creates a new multibinder with a binding annotation.
     * @param binder Binder to configure
     * @param type Element type
     * @param annotation Binding annotation
     * @return New Multibinder instance
     */
    public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T> type, Annotation annotation);
    
    /**
     * Configures the bound set to silently discard duplicate elements.
     * @return This multibinder instance
     */
    public Multibinder<T> permitDuplicates();
    
    /**
     * Returns a binding builder used to add a new element to the set.
     * @return Binding builder for adding elements
     */
    public LinkedBindingBuilder<T> addBinding();
}

MapBinder Class

Binds multiple map entries separately to be injected as a complete Map<K,V>.

/**
 * An API to bind multiple map entries separately, only to later inject them as
 * a complete map. MapBinder is intended for use in your application's module.
 * @param <K> Map key type
 * @param <V> Map value type
 */
public class MapBinder<K, V> {
    /**
     * Creates a new map binder that collects entries into a Map<K,V>.
     * @param binder Binder to configure
     * @param keyType Key type
     * @param valueType Value type
     * @return New MapBinder instance
     */
    public static <K, V> MapBinder<K, V> newMapBinder(Binder binder, Class<K> keyType, Class<V> valueType);
    
    /**
     * Creates a new map binder that collects entries into a Map<K,V>.
     * @param binder Binder to configure  
     * @param keyType Key type literal
     * @param valueType Value type literal
     * @return New MapBinder instance
     */
    public static <K, V> MapBinder<K, V> newMapBinder(Binder binder, TypeLiteral<K> keyType, TypeLiteral<V> valueType);
    
    /**
     * Configures the bound map to silently ignore duplicate entries.
     * @return This MapBinder instance
     */
    public MapBinder<K, V> permitDuplicates();
    
    /**
     * Returns a binding builder used to add a new entry to the map.
     * @param key Map key
     * @return Binding builder for the value
     */
    public LinkedBindingBuilder<V> addBinding(K key);
}

Usage Examples:

// Set multibinding
public class PluginModule extends AbstractModule {
    @Override
    protected void configure() {
        Multibinder<Plugin> pluginBinder = Multibinder.newSetBinder(binder(), Plugin.class);
        pluginBinder.addBinding().to(DatabasePlugin.class);
        pluginBinder.addBinding().to(CachePlugin.class);
        pluginBinder.addBinding().to(LoggingPlugin.class);
    }
}

// Map multibinding
public class ConfigModule extends AbstractModule {
    @Override
    protected void configure() {
        MapBinder<String, DatabaseConfig> mapBinder = MapBinder.newMapBinder(
            binder(), String.class, DatabaseConfig.class);
        mapBinder.addBinding("primary").toInstance(new DatabaseConfig("primary-db"));
        mapBinder.addBinding("secondary").toInstance(new DatabaseConfig("secondary-db"));
    }
}

// Injection
public class Application {
    @Inject Set<Plugin> plugins;
    @Inject Map<String, DatabaseConfig> databases;
}

OptionalBinder Class

Binds optional values with possible defaults, injected as Optional<T>.

/**
 * An API to bind optional values. OptionalBinder enables injecting dependencies
 * that may or may not be present, with optional default values.
 * @param <T> Type of the optional value
 */
public class OptionalBinder<T> {
    /**
     * Creates a new optional binder.
     * @param binder Binder to configure
     * @param type Value type
     * @return New OptionalBinder instance
     */
    public static <T> OptionalBinder<T> newOptionalBinder(Binder binder, Class<T> type);
    
    /**
     * Sets the default binding for this optional value.
     * @return Binding builder for the default value
     */
    public LinkedBindingBuilder<T> setDefault();
    
    /**
     * Sets the actual binding for this optional value.
     * @return Binding builder for the actual value
     */
    public LinkedBindingBuilder<T> setBinding();
}

Install with Tessl CLI

npx tessl i tessl/maven-com-google-inject--guice

docs

advanced.md

annotations.md

core-injection.md

index.md

modules.md

multibindings.md

providers-scopes.md

spi.md

types-keys.md

tile.json