CtrlK
BlogDocsLog inGet started
Tessl Logo

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

Pending
Overview
Eval results
Files

component-framework.mddocs/

Component Framework

The component framework is the core of Dagger's dependency injection system. Components define the dependency injection container and specify how dependencies are provided and injected throughout the application.

Capabilities

@Component Annotation

Annotates interfaces or abstract classes for which Dagger generates fully-formed, dependency-injected implementations. The generated implementation has a name prefixed with "Dagger".

/**
 * Annotates interfaces or abstract classes for which Dagger generates dependency-injected implementations
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Component {
    /**
     * Array of Module classes that contribute bindings to this component
     */
    Class<?>[] modules() default {};
    
    /**
     * Array of component dependency types this component depends on
     */
    Class<?>[] dependencies() default {};
}

Usage Examples:

// Simple component
@Component(modules = DatabaseModule.class)
public interface ApplicationComponent {
    UserService userService();
}

// Component with dependencies
@Component(modules = NetworkModule.class, dependencies = ConfigComponent.class)
public interface ApiComponent {
    ApiService apiService();
}

// Generated implementation usage
ApplicationComponent component = DaggerApplicationComponent.create();
UserService service = component.userService();

@Component.Builder

Enables the builder pattern for component creation, allowing configuration of modules and dependencies before component instantiation.

/**
 * Annotates types that serve as builders for Component instances
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Component.Builder {}

Usage Examples:

@Component(modules = DatabaseModule.class)
public interface ApplicationComponent {
    UserService userService();
    
    @Component.Builder
    interface Builder {
        Builder databaseModule(DatabaseModule module);
        ApplicationComponent build();
    }
}

// Usage with builder
ApplicationComponent component = DaggerApplicationComponent.builder()
    .databaseModule(new DatabaseModule("prod-db"))
    .build();

@Component.Factory

Enables the factory pattern for component creation (available since Dagger 2.22), providing a more concise alternative to builders.

/**
 * Annotates types that serve as factories for Component instances
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Component.Factory {}

Usage Examples:

@Component(modules = DatabaseModule.class)
public interface ApplicationComponent {
    UserService userService();
    
    @Component.Factory
    interface Factory {
        ApplicationComponent create(DatabaseModule databaseModule);
    }
}

// Usage with factory
ApplicationComponent component = DaggerApplicationComponent.factory()
    .create(new DatabaseModule("prod-db"));

@Subcomponent Annotation

Creates a subcomponent that inherits the entire binding graph from its parent Component or Subcomponent. Subcomponents can add additional bindings and have their own scope.

/**
 * Annotates interfaces or abstract classes that extend the binding graph of a parent component
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Subcomponent {
    /**
     * Array of Module classes that contribute additional bindings to this subcomponent
     */
    Class<?>[] modules() default {};
}

Usage Examples:

// Parent component
@Component(modules = ApplicationModule.class)
@Singleton
public interface ApplicationComponent {
    UserSubcomponent.Factory userSubcomponentFactory();
}

// Subcomponent
@Subcomponent(modules = UserModule.class)
@UserScope
public interface UserSubcomponent {
    UserService userService();
    void inject(UserActivity activity);
    
    @Subcomponent.Factory
    interface Factory {
        UserSubcomponent create(User user);
    }
}

// Usage
ApplicationComponent appComponent = DaggerApplicationComponent.create();
UserSubcomponent userComponent = appComponent.userSubcomponentFactory()
    .create(currentUser);

@Subcomponent.Builder

Enables the builder pattern for subcomponent creation.

/**
 * Annotates types that serve as builders for Subcomponent instances
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Subcomponent.Builder {}

@Subcomponent.Factory

Enables the factory pattern for subcomponent creation.

/**
 * Annotates types that serve as factories for Subcomponent instances
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Subcomponent.Factory {}

Component Methods

Components support two types of methods:

Provision Methods:

  • No parameters
  • Return type is the injected type
  • Dagger provides an instance of the return type

Members-Injection Methods:

  • Single parameter (the object to inject)
  • Return type is void or the parameter type
  • Injects dependencies into the parameter object
@Component(modules = ServiceModule.class)
public interface AppComponent {
    // Provision method
    UserService getUserService();
    
    // Members-injection method
    void inject(MainActivity activity);
    
    // Members-injection method with return
    MainActivity inject(MainActivity activity);
}

Instance Binding

Components can bind instances directly using @BindsInstance in builders or factories.

/**
 * Marks methods in Component.Builder or parameters in Component.Factory for binding instances
 * @Beta This API is subject to incompatible changes
 */
@Target({ElementType.METHOD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@interface BindsInstance {}

Usage Examples:

@Component(modules = NetworkModule.class)
public interface NetworkComponent {
    ApiService apiService();
    
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder baseUrl(String baseUrl);
        
        @BindsInstance  
        Builder timeout(@Named("timeout") int timeoutSeconds);
        
        NetworkComponent build();
    }
}

// Usage
NetworkComponent component = DaggerNetworkComponent.builder()
    .baseUrl("https://api.example.com")
    .timeout(30)
    .build();

Component Hierarchies

Components can depend on other components, creating a hierarchy where child components can access bindings from parent components.

// Configuration component (parent)
@Component(modules = ConfigModule.class)
@Singleton
public interface ConfigComponent {
    DatabaseConfig databaseConfig();
    ApiConfig apiConfig();
}

// Application component (child)
@Component(
    modules = {DatabaseModule.class, ApiModule.class},
    dependencies = ConfigComponent.class
)
@ApplicationScope
public interface ApplicationComponent {
    UserService userService();
    ApiService apiService();
}

// Usage
ConfigComponent configComponent = DaggerConfigComponent.create();
ApplicationComponent appComponent = DaggerApplicationComponent.builder()
    .configComponent(configComponent)
    .build();

Install with Tessl CLI

npx tessl i tessl/maven-com-google-dagger--dagger

docs

assisted-injection.md

component-framework.md

index.md

module-system.md

multibindings.md

utility-types.md

tile.json