Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsJul 26, 2026·3 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Guice Overview
Comando de instalación directa
npx -y tokrepo@latest install ce6ed879-8930-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con dry-run.

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

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados