Google Guice

repository·master·Indexed 11 days ago

https://github.com/google/guice

A lightweight dependency injection framework for Java that replaces manual factory patterns with annotation-based injection. Supports versions 6.0.0 for javax namespaces and 7.0.0 for jakarta namespaces, with various extensions including assistedinject, dagger-adapter, and spring.

Tokens
608
Snippets
2
Records
3
Agent score
46%

What's inside Guice

  1. What is Guice? (Core Concepts)

    master

    Guice is a dependency injection framework that alleviates the need for manual factory patterns and the explicit use of the new keyword in your Java code.

    Key mental models:

    • @Inject as the new new: Instead of manually instantiating objects, you use the @Inject annotation to request dependencies. Guice handles the instantiation and wiring.
    • Type Safety: Guice leverages Java's type system to ensure dependencies are correctly wired.
    • Decoupling: By using Guice, your code does not depend directly on specific factory implementations, making it easier to change, unit test, and reuse.
  2. Install Guice Extensions via Maven

    master

    Guice provides several extensions for specific use cases. When installing an extension, the version must match the version of Guice core you are using.

    Available extensions ({extension-name}):

    • assistedinject
    • dagger-adapter
    • grapher
    • jmx
    • jndi
    • persist
    • spring
    • testlib
    • throwingproviders
    <dependency>
      <groupId>com.google.inject.extensions</groupId>
      <!-- {extension-name} can be one of: assistedinject, dagger-adapter, 
           grapher, jmx, jndi, persist, spring, testlib or throwingproviders -->
      <artifactId>guice-{extension-name}</artifactId>
      <!-- {version} must match the guice core version. -->
      <version>{version}</version>
    </dependency>
  3. Install Guice Core via Maven

    master

    To use Guice in your project, add the guice dependency to your Maven pom.xml. Choose the version based on your requirements for javax or jakarta namespace support:

    • Use 6.0.0 if you need support for javax.{inject,servlet,persistence} (and partial support for jakarta.inject).
    • Use 7.0.0 if you are using jakarta.{inject,servlet,persistence}.
    <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <!-- {version} can be 6.0.0, 7.0.0, etc. -->
      <version>{version}</version>
    </dependency>