or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

assisted-injection.mdcomponent-framework.mdindex.mdmodule-system.mdmultibindings.mdutility-types.md
tile.json

tessl/maven-com-google-dagger--dagger

A fast dependency injector for Java and Android that generates plain Java source code without using reflection or runtime bytecode generation

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.google.dagger/dagger@2.56.x

To install, run

npx @tessl/cli install tessl/maven-com-google-dagger--dagger@2.56.0

index.mddocs/

Dagger

Dagger is a compile-time dependency injection framework for Java and Android that generates plain Java source code without using reflection or runtime bytecode generation. It provides a comprehensive set of annotations and components for defining and managing object dependencies, performing all dependency analysis at build time to generate efficient dependency injection code with full compile-time validation.

Package Information

  • Package Name: com.google.dagger:dagger
  • Package Type: maven
  • Language: Java
  • Installation:
    <dependency>
      <groupId>com.google.dagger</groupId>
      <artifactId>dagger</artifactId>
      <version>2.56.2</version>
    </dependency>

Core Imports

import dagger.Component;
import dagger.Module;
import dagger.Provides;
import dagger.Binds;
import dagger.Subcomponent;
import dagger.Lazy;
import dagger.MembersInjector;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.inject.Qualifier;
import javax.inject.Scope;

Basic Usage

import dagger.*;
import javax.inject.Inject;
import javax.inject.Singleton;

// Define a module
@Module
public class DatabaseModule {
    @Provides
    @Singleton
    DatabaseService provideDatabaseService() {
        return new DatabaseService("localhost", 5432);
    }
}

// Define a component
@Component(modules = DatabaseModule.class)
@Singleton
public interface ApplicationComponent {
    UserRepository userRepository();
    void inject(MainActivity activity);
}

// Injectable class
public class UserRepository {
    private final DatabaseService databaseService;
    
    @Inject
    public UserRepository(DatabaseService databaseService) {
        this.databaseService = databaseService;
    }
}

// Usage
ApplicationComponent component = DaggerApplicationComponent.create();
UserRepository repository = component.userRepository();

Architecture

Dagger is built around several key concepts:

  • Components: Interfaces that define the dependency injection container and how dependencies are provided
  • Modules: Classes that define how dependencies are constructed using provider methods
  • Dependency Graph: Compile-time analysis of all dependencies and their relationships
  • Code Generation: Plain Java source code generated at compile time (no reflection)
  • Scoping: Lifecycle management of objects through scoping annotations
  • Multibindings: Support for injecting collections (Set, Map) of related dependencies

Capabilities

Component Framework

Core dependency injection container system defining how dependencies are provided and injected. Components serve as the main entry points for dependency injection.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Component {
    Class<?>[] modules() default {};
    Class<?>[] dependencies() default {};
    
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Builder {}
    
    @Target(ElementType.TYPE) 
    @Retention(RetentionPolicy.RUNTIME)
    @interface Factory {}
}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Subcomponent {
    Class<?>[] modules() default {};
    
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Builder {}
    
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Factory {}
}

Component Framework

Module System

Module system for defining how dependencies are constructed and provided to the dependency injection container through provider methods and binding methods.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Module {
    Class<?>[] includes() default {};
    Class<?>[] subcomponents() default {};
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Provides {}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Binds {}

Module System

Multibindings

Advanced binding system for injecting collections (Set, Map) of related dependencies, allowing multiple modules to contribute elements to the same collection.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface IntoSet {}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface IntoMap {}

@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MapKey {
    boolean unwrapValue() default true;
}

Multibindings

Assisted Injection

Specialized injection pattern for objects that require both injected dependencies and runtime parameters, using factory patterns to create instances with mixed parameter sources.

@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
@interface AssistedInject {}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface AssistedFactory {}

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@interface Assisted {
    String value() default "";
}

Assisted Injection

Utility Types

Core utility interfaces and classes providing lazy evaluation, members injection, and other dependency injection utilities.

interface Lazy<T> {
    T get();
}

interface MembersInjector<T> {
    void injectMembers(T instance);
}

Utility Types

Types

Core JSR-330 Types

// From javax.inject package
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface Inject {}

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Scope
@interface Singleton {}

@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Scope {}

@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Qualifier {}

interface Provider<T> {
    T get();
}

Dagger Scoping

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Scope
@interface Reusable {}

Note: @Reusable is marked as @Beta and subject to incompatible changes.