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
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Google Guice

Google Guice is a lightweight dependency injection framework for Java 8 and above that eliminates the need for factories and the use of 'new' in Java code. It embraces Java's type-safe nature and provides runtime dependency resolution with helpful error messages.

Package Information

  • Package Name: com.google.inject:guice
  • Package Type: maven
  • Language: Java
  • Version: 7.0.0
  • Installation: Add to pom.xml: <dependency><groupId>com.google.inject</groupId><artifactId>guice</artifactId><version>7.0.0</version></dependency>

Core Imports

import com.google.inject.*;
import com.google.inject.name.Named;

Basic Usage

import com.google.inject.*;

// Define an interface and implementation
interface MessageService {
    String getMessage();
}

class EmailService implements MessageService {
    public String getMessage() {
        return "Email message";
    }
}

// Create a module to configure bindings
class AppModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(MessageService.class).to(EmailService.class);
    }
}

// Create injector and get instances
public class MyApp {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new AppModule());
        MessageService service = injector.getInstance(MessageService.class);
        System.out.println(service.getMessage());
    }
}

Architecture

Guice is built around several key components:

  • Injector: The core dependency injection engine that creates and provides instances
  • Module: Configuration units that define how dependencies should be resolved
  • Binder: API for configuring dependency bindings within modules
  • Key & TypeLiteral: Type-safe identifiers for dependencies including generic types
  • Provider: Custom factories for creating instances with complex initialization logic
  • Scopes: Lifecycle management for instances (Singleton, etc.)
  • Annotations: Markers for injection points (@Inject) and binding qualifiers (@Named)

Capabilities

Core Dependency Injection

Essential classes for creating injectors, configuring modules, and injecting dependencies. This is the primary API that most applications will use.

public final class Guice {
    public static Injector createInjector(Module... modules);
    public static Injector createInjector(Stage stage, Module... modules);
}

public interface Injector {
    <T> T getInstance(Class<T> type);
    <T> T getInstance(Key<T> key);
    <T> Provider<T> getProvider(Class<T> type);
    void injectMembers(Object instance);
    Injector createChildInjector(Module... modules);
}

Core Dependency Injection

Module Configuration

Classes and interfaces for defining how dependencies are bound, including the fluent binding DSL and provider methods.

public interface Module {
    void configure(Binder binder);
}

public abstract class AbstractModule implements Module {
    protected <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz);
    protected AnnotatedConstantBindingBuilder bindConstant();
    protected void install(Module module);
}

Module Configuration

Type System & Keys

Type-safe dependency identification system supporting generic types and binding annotations for distinguishing multiple bindings of the same type.

public class Key<T> {
    public static <T> Key<T> get(Class<T> type);
    public static <T> Key<T> get(Class<T> type, Class<? extends Annotation> annotationType);
    public static <T> Key<T> get(Class<T> type, Annotation annotation);
}

public class TypeLiteral<T> {
    public static <T> TypeLiteral<T> get(Class<T> type);
    public Class<? super T> getRawType();
    public Type getType();
}

Type System & Keys

Providers & Scopes

Custom instance creation and lifecycle management including built-in scopes and provider interfaces for complex object construction.

public interface Provider<T> {
    T get();
}

public interface Scope {
    <T> Provider<T> scope(Key<T> key, Provider<T> unscoped);
}

public final class Scopes {
    public static final Scope SINGLETON;
    public static final Scope NO_SCOPE;
}

Providers & Scopes

Essential Annotations

Key annotations for marking injection points, scoping instances, and creating binding qualifiers.

@Target({METHOD, CONSTRUCTOR, FIELD})
@Retention(RUNTIME)
public @interface Inject {
    boolean optional() default false;
}

@Target({TYPE, METHOD})
@Retention(RUNTIME)
@ScopeAnnotation
public @interface Singleton {}

@Target(METHOD)
@Retention(RUNTIME)
public @interface Provides {}

Essential Annotations

Multibindings

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

public class Multibinder<T> {
    public static <T> Multibinder<T> newSetBinder(Binder binder, Class<T> type);
    public LinkedBindingBuilder<T> addBinding();
}

public class MapBinder<K, V> {
    public static <K, V> MapBinder<K, V> newMapBinder(Binder binder, Class<K> keyType, Class<V> valueType);
    public LinkedBindingBuilder<V> addBinding(K key);
}

Multibindings

Advanced Features

AOP interceptors, method matching, private modules, and other advanced dependency injection patterns.

public interface Matcher<T> {
    boolean matches(T t);
}

public final class Matchers {
    public static Matcher<Class> subclassesOf(Class<?> superclass);
    public static Matcher<AnnotatedElement> annotatedWith(Class<? extends Annotation> annotationType);
}

public abstract class PrivateModule implements Module {
    protected void expose(Class<?> type);
    protected void expose(Key<?> key);
}

Advanced Features

Service Provider Interface (SPI)

Introspection and tooling APIs for analyzing injector configuration, visiting bindings, and integrating with development tools.

public final class Elements {
    public static List<Element> getElements(Module... modules);
    public static Module getModule(Iterable<? extends Element> elements);
}

public interface Element {
    Object getSource();
    <T> T acceptVisitor(ElementVisitor<T> visitor);
    void applyTo(Binder binder);
}

Service Provider Interface

docs

advanced.md

annotations.md

core-injection.md

index.md

modules.md

multibindings.md

providers-scopes.md

spi.md

types-keys.md

tile.json