A fast dependency injector for Java and Android that generates plain Java source code without using reflection or runtime bytecode generation
npx @tessl/cli install tessl/maven-com-google-dagger--dagger@2.56.0Dagger 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.
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger</artifactId>
<version>2.56.2</version>
</dependency>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;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();Dagger is built around several key concepts:
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 {}
}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 {}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;
}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 "";
}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);
}// 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();
}@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Scope
@interface Reusable {}Note: @Reusable is marked as @Beta and subject to incompatible changes.