Configs2026年7月26日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Guice Overview
直接安装命令
npx -y tokrepo@latest install ce6ed879-8930-11f1-9bc6-00163e2b0d79 --target codex

先 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产