or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

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

index.mddocs/

0

# Dagger

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: com.google.dagger:dagger

7

- **Package Type**: maven

8

- **Language**: Java

9

- **Installation**:

10

```xml

11

<dependency>

12

<groupId>com.google.dagger</groupId>

13

<artifactId>dagger</artifactId>

14

<version>2.56.2</version>

15

</dependency>

16

```

17

18

## Core Imports

19

20

```java

21

import dagger.Component;

22

import dagger.Module;

23

import dagger.Provides;

24

import dagger.Binds;

25

import dagger.Subcomponent;

26

import dagger.Lazy;

27

import dagger.MembersInjector;

28

import javax.inject.Inject;

29

import javax.inject.Provider;

30

import javax.inject.Singleton;

31

import javax.inject.Qualifier;

32

import javax.inject.Scope;

33

```

34

35

## Basic Usage

36

37

```java

38

import dagger.*;

39

import javax.inject.Inject;

40

import javax.inject.Singleton;

41

42

// Define a module

43

@Module

44

public class DatabaseModule {

45

@Provides

46

@Singleton

47

DatabaseService provideDatabaseService() {

48

return new DatabaseService("localhost", 5432);

49

}

50

}

51

52

// Define a component

53

@Component(modules = DatabaseModule.class)

54

@Singleton

55

public interface ApplicationComponent {

56

UserRepository userRepository();

57

void inject(MainActivity activity);

58

}

59

60

// Injectable class

61

public class UserRepository {

62

private final DatabaseService databaseService;

63

64

@Inject

65

public UserRepository(DatabaseService databaseService) {

66

this.databaseService = databaseService;

67

}

68

}

69

70

// Usage

71

ApplicationComponent component = DaggerApplicationComponent.create();

72

UserRepository repository = component.userRepository();

73

```

74

75

## Architecture

76

77

Dagger is built around several key concepts:

78

79

- **Components**: Interfaces that define the dependency injection container and how dependencies are provided

80

- **Modules**: Classes that define how dependencies are constructed using provider methods

81

- **Dependency Graph**: Compile-time analysis of all dependencies and their relationships

82

- **Code Generation**: Plain Java source code generated at compile time (no reflection)

83

- **Scoping**: Lifecycle management of objects through scoping annotations

84

- **Multibindings**: Support for injecting collections (Set, Map) of related dependencies

85

86

## Capabilities

87

88

### Component Framework

89

90

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

91

92

```java { .api }

93

@Target(ElementType.TYPE)

94

@Retention(RetentionPolicy.RUNTIME)

95

@interface Component {

96

Class<?>[] modules() default {};

97

Class<?>[] dependencies() default {};

98

99

@Target(ElementType.TYPE)

100

@Retention(RetentionPolicy.RUNTIME)

101

@interface Builder {}

102

103

@Target(ElementType.TYPE)

104

@Retention(RetentionPolicy.RUNTIME)

105

@interface Factory {}

106

}

107

108

@Target(ElementType.TYPE)

109

@Retention(RetentionPolicy.RUNTIME)

110

@interface Subcomponent {

111

Class<?>[] modules() default {};

112

113

@Target(ElementType.TYPE)

114

@Retention(RetentionPolicy.RUNTIME)

115

@interface Builder {}

116

117

@Target(ElementType.TYPE)

118

@Retention(RetentionPolicy.RUNTIME)

119

@interface Factory {}

120

}

121

```

122

123

[Component Framework](./component-framework.md)

124

125

### Module System

126

127

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

128

129

```java { .api }

130

@Target(ElementType.TYPE)

131

@Retention(RetentionPolicy.RUNTIME)

132

@interface Module {

133

Class<?>[] includes() default {};

134

Class<?>[] subcomponents() default {};

135

}

136

137

@Target(ElementType.METHOD)

138

@Retention(RetentionPolicy.RUNTIME)

139

@interface Provides {}

140

141

@Target(ElementType.METHOD)

142

@Retention(RetentionPolicy.RUNTIME)

143

@interface Binds {}

144

```

145

146

[Module System](./module-system.md)

147

148

### Multibindings

149

150

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

151

152

```java { .api }

153

@Target(ElementType.METHOD)

154

@Retention(RetentionPolicy.RUNTIME)

155

@interface IntoSet {}

156

157

@Target(ElementType.METHOD)

158

@Retention(RetentionPolicy.RUNTIME)

159

@interface IntoMap {}

160

161

@Target(ElementType.ANNOTATION_TYPE)

162

@Retention(RetentionPolicy.RUNTIME)

163

@interface MapKey {

164

boolean unwrapValue() default true;

165

}

166

```

167

168

[Multibindings](./multibindings.md)

169

170

### Assisted Injection

171

172

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

173

174

```java { .api }

175

@Target(ElementType.CONSTRUCTOR)

176

@Retention(RetentionPolicy.RUNTIME)

177

@interface AssistedInject {}

178

179

@Target(ElementType.TYPE)

180

@Retention(RetentionPolicy.RUNTIME)

181

@interface AssistedFactory {}

182

183

@Target(ElementType.PARAMETER)

184

@Retention(RetentionPolicy.RUNTIME)

185

@interface Assisted {

186

String value() default "";

187

}

188

```

189

190

[Assisted Injection](./assisted-injection.md)

191

192

### Utility Types

193

194

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

195

196

```java { .api }

197

interface Lazy<T> {

198

T get();

199

}

200

201

interface MembersInjector<T> {

202

void injectMembers(T instance);

203

}

204

```

205

206

[Utility Types](./utility-types.md)

207

208

## Types

209

210

### Core JSR-330 Types

211

212

```java { .api }

213

// From javax.inject package

214

@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})

215

@Retention(RetentionPolicy.RUNTIME)

216

@interface Inject {}

217

218

@Target({ElementType.TYPE, ElementType.METHOD})

219

@Retention(RetentionPolicy.RUNTIME)

220

@Scope

221

@interface Singleton {}

222

223

@Target(ElementType.ANNOTATION_TYPE)

224

@Retention(RetentionPolicy.RUNTIME)

225

@interface Scope {}

226

227

@Target(ElementType.ANNOTATION_TYPE)

228

@Retention(RetentionPolicy.RUNTIME)

229

@interface Qualifier {}

230

231

interface Provider<T> {

232

T get();

233

}

234

```

235

236

### Dagger Scoping

237

238

```java { .api }

239

@Target({ElementType.TYPE, ElementType.METHOD})

240

@Retention(RetentionPolicy.RUNTIME)

241

@Scope

242

@interface Reusable {}

243

```

244

245

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