# Google Guice — Lightweight Dependency Injection for Java > A mature dependency injection framework by Google for Java 11 and above that reduces boilerplate, improves testability, and simplifies wiring of large applications. ## Install Save in your project root: # Google Guice — Lightweight Dependency Injection for Java ## Quick Use ```xml com.google.inject guice 7.0.0 ``` ```java Injector injector = Guice.createInjector(new AppModule()); MyService service = injector.getInstance(MyService.class); ``` ## Introduction Google Guice (pronounced "juice") is a lightweight dependency injection framework for Java that uses annotations and programmatic configuration instead of XML. It was created at Google to manage the complexity of large-scale Java applications and has been used internally across many Google products since 2006. ## What Guice Does - Wires together object graphs using constructor, method, and field injection - Uses standard `@Inject` annotations compatible with JSR-330 - Provides programmatic module configuration through a fluent Java API - Supports scoping (singleton, request, session) for managing object lifecycles - Enables aspect-oriented programming via method interception ## Architecture Overview Guice builds an injector from one or more modules that define bindings between interfaces and implementations. At startup, the injector validates the entire object graph, catching missing or circular dependencies before any business logic runs. At runtime, it constructs objects by recursively resolving their annotated dependencies, caching singletons as configured. ## Self-Hosting & Configuration - Add the Guice dependency via Maven or Gradle to your Java project - Define modules by extending `AbstractModule` and overriding `configure()` - Bind interfaces to implementations using `bind(Foo.class).to(FooImpl.class)` - Use `@Provides` methods for complex object construction logic - Bootstrap with `Guice.createInjector(new Module1(), new Module2())` ## Key Features - Compile-time-like safety: the injector validates the full dependency graph at startup - No XML configuration required; everything is plain Java code - Built-in support for `@Singleton`, `@RequestScoped`, and custom scopes - AOP integration for cross-cutting concerns like logging and transactions - Small footprint with minimal transitive dependencies ## Comparison with Similar Tools - **Spring Framework** — full-featured but heavier; Guice focuses exclusively on DI with a smaller API surface - **Dagger 2** — compile-time DI with code generation; Guice uses runtime reflection for more flexibility - **CDI (Jakarta)** — Java EE standard with broader scope; Guice is standalone and lighter for non-EE projects - **PicoContainer** — simpler constructor injection; Guice offers richer scoping and AOP support ## FAQ **Q: Is Guice still actively maintained?** A: Yes. Google continues to maintain Guice, with recent releases supporting Java 11+ and modern language features. **Q: How does Guice compare to Dagger for Android development?** A: Dagger generates code at compile time, making it faster at runtime, which matters on mobile. Guice uses reflection and is better suited for server-side applications where startup time is less critical. **Q: Can Guice be used alongside Spring?** A: Yes, though it is uncommon. Guice can manage DI for specific subsystems within a Spring application if needed. **Q: Does Guice support constructor injection without annotations?** A: Guice requires the `@Inject` annotation on constructors (or a single public no-arg constructor). It does not do implicit constructor discovery. ## Sources - https://github.com/google/guice - https://github.com/google/guice/wiki --- Source: https://tokrepo.com/en/workflows/asset-ce6ed879 Author: AI Open Source