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

component-framework.mddocs/

0

# Component Framework

1

2

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.

3

4

## Capabilities

5

6

### @Component Annotation

7

8

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

9

10

```java { .api }

11

/**

12

* Annotates interfaces or abstract classes for which Dagger generates dependency-injected implementations

13

*/

14

@Target(ElementType.TYPE)

15

@Retention(RetentionPolicy.RUNTIME)

16

@interface Component {

17

/**

18

* Array of Module classes that contribute bindings to this component

19

*/

20

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

21

22

/**

23

* Array of component dependency types this component depends on

24

*/

25

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

26

}

27

```

28

29

**Usage Examples:**

30

31

```java

32

// Simple component

33

@Component(modules = DatabaseModule.class)

34

public interface ApplicationComponent {

35

UserService userService();

36

}

37

38

// Component with dependencies

39

@Component(modules = NetworkModule.class, dependencies = ConfigComponent.class)

40

public interface ApiComponent {

41

ApiService apiService();

42

}

43

44

// Generated implementation usage

45

ApplicationComponent component = DaggerApplicationComponent.create();

46

UserService service = component.userService();

47

```

48

49

### @Component.Builder

50

51

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

52

53

```java { .api }

54

/**

55

* Annotates types that serve as builders for Component instances

56

*/

57

@Target(ElementType.TYPE)

58

@Retention(RetentionPolicy.RUNTIME)

59

@interface Component.Builder {}

60

```

61

62

**Usage Examples:**

63

64

```java

65

@Component(modules = DatabaseModule.class)

66

public interface ApplicationComponent {

67

UserService userService();

68

69

@Component.Builder

70

interface Builder {

71

Builder databaseModule(DatabaseModule module);

72

ApplicationComponent build();

73

}

74

}

75

76

// Usage with builder

77

ApplicationComponent component = DaggerApplicationComponent.builder()

78

.databaseModule(new DatabaseModule("prod-db"))

79

.build();

80

```

81

82

### @Component.Factory

83

84

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

85

86

```java { .api }

87

/**

88

* Annotates types that serve as factories for Component instances

89

*/

90

@Target(ElementType.TYPE)

91

@Retention(RetentionPolicy.RUNTIME)

92

@interface Component.Factory {}

93

```

94

95

**Usage Examples:**

96

97

```java

98

@Component(modules = DatabaseModule.class)

99

public interface ApplicationComponent {

100

UserService userService();

101

102

@Component.Factory

103

interface Factory {

104

ApplicationComponent create(DatabaseModule databaseModule);

105

}

106

}

107

108

// Usage with factory

109

ApplicationComponent component = DaggerApplicationComponent.factory()

110

.create(new DatabaseModule("prod-db"));

111

```

112

113

### @Subcomponent Annotation

114

115

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.

116

117

```java { .api }

118

/**

119

* Annotates interfaces or abstract classes that extend the binding graph of a parent component

120

*/

121

@Target(ElementType.TYPE)

122

@Retention(RetentionPolicy.RUNTIME)

123

@interface Subcomponent {

124

/**

125

* Array of Module classes that contribute additional bindings to this subcomponent

126

*/

127

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

128

}

129

```

130

131

**Usage Examples:**

132

133

```java

134

// Parent component

135

@Component(modules = ApplicationModule.class)

136

@Singleton

137

public interface ApplicationComponent {

138

UserSubcomponent.Factory userSubcomponentFactory();

139

}

140

141

// Subcomponent

142

@Subcomponent(modules = UserModule.class)

143

@UserScope

144

public interface UserSubcomponent {

145

UserService userService();

146

void inject(UserActivity activity);

147

148

@Subcomponent.Factory

149

interface Factory {

150

UserSubcomponent create(User user);

151

}

152

}

153

154

// Usage

155

ApplicationComponent appComponent = DaggerApplicationComponent.create();

156

UserSubcomponent userComponent = appComponent.userSubcomponentFactory()

157

.create(currentUser);

158

```

159

160

### @Subcomponent.Builder

161

162

Enables the builder pattern for subcomponent creation.

163

164

```java { .api }

165

/**

166

* Annotates types that serve as builders for Subcomponent instances

167

*/

168

@Target(ElementType.TYPE)

169

@Retention(RetentionPolicy.RUNTIME)

170

@interface Subcomponent.Builder {}

171

```

172

173

### @Subcomponent.Factory

174

175

Enables the factory pattern for subcomponent creation.

176

177

```java { .api }

178

/**

179

* Annotates types that serve as factories for Subcomponent instances

180

*/

181

@Target(ElementType.TYPE)

182

@Retention(RetentionPolicy.RUNTIME)

183

@interface Subcomponent.Factory {}

184

```

185

186

### Component Methods

187

188

Components support two types of methods:

189

190

**Provision Methods:**

191

- No parameters

192

- Return type is the injected type

193

- Dagger provides an instance of the return type

194

195

**Members-Injection Methods:**

196

- Single parameter (the object to inject)

197

- Return type is void or the parameter type

198

- Injects dependencies into the parameter object

199

200

```java

201

@Component(modules = ServiceModule.class)

202

public interface AppComponent {

203

// Provision method

204

UserService getUserService();

205

206

// Members-injection method

207

void inject(MainActivity activity);

208

209

// Members-injection method with return

210

MainActivity inject(MainActivity activity);

211

}

212

```

213

214

### Instance Binding

215

216

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

217

218

```java { .api }

219

/**

220

* Marks methods in Component.Builder or parameters in Component.Factory for binding instances

221

* @Beta This API is subject to incompatible changes

222

*/

223

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

224

@Retention(RetentionPolicy.RUNTIME)

225

@interface BindsInstance {}

226

```

227

228

**Usage Examples:**

229

230

```java

231

@Component(modules = NetworkModule.class)

232

public interface NetworkComponent {

233

ApiService apiService();

234

235

@Component.Builder

236

interface Builder {

237

@BindsInstance

238

Builder baseUrl(String baseUrl);

239

240

@BindsInstance

241

Builder timeout(@Named("timeout") int timeoutSeconds);

242

243

NetworkComponent build();

244

}

245

}

246

247

// Usage

248

NetworkComponent component = DaggerNetworkComponent.builder()

249

.baseUrl("https://api.example.com")

250

.timeout(30)

251

.build();

252

```

253

254

### Component Hierarchies

255

256

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

257

258

```java

259

// Configuration component (parent)

260

@Component(modules = ConfigModule.class)

261

@Singleton

262

public interface ConfigComponent {

263

DatabaseConfig databaseConfig();

264

ApiConfig apiConfig();

265

}

266

267

// Application component (child)

268

@Component(

269

modules = {DatabaseModule.class, ApiModule.class},

270

dependencies = ConfigComponent.class

271

)

272

@ApplicationScope

273

public interface ApplicationComponent {

274

UserService userService();

275

ApiService apiService();

276

}

277

278

// Usage

279

ConfigComponent configComponent = DaggerConfigComponent.create();

280

ApplicationComponent appComponent = DaggerApplicationComponent.builder()

281

.configComponent(configComponent)

282

.build();

283

```