Lightweight dependency injection framework for Java 8 and above that eliminates factories and the use of 'new' through @Inject annotation
npx @tessl/cli install tessl/maven-com-google-inject--guice@7.0.0Google 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.
<dependency><groupId>com.google.inject</groupId><artifactId>guice</artifactId><version>7.0.0</version></dependency>import com.google.inject.*;
import com.google.inject.name.Named;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());
}
}Guice is built around several key components:
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);
}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);
}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();
}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;
}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 {}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);
}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);
}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);
}