A Firebase Component is a blueprint used by the Firebase Component Model to discover, resolve dependencies, and instantiate SDK services via Dependency Injection.
Key characteristics of a Component:
- Interfaces: It implements one or more interfaces.
- Dependencies: It declares required or optional dependencies on other components.
- Initialization: It specifies whether it should be initialized eagerly (at app startup) or lazily (on demand).
- Factory: It defines a factory that creates the component instance using its resolved dependencies.
Components are singletons within a FirebaseApp container. For SDKs requiring multiple instances (like multiple databases in RTDB or Firestore), the component registers a 'MultiResource' singleton that manages and provides multiple instances per resource name.
// Defines a component that is registered as both `FirebaseAuth` and `InternalAuthProvider`.
Component<FirebaseAuth> auth = Component.builder(FirebaseAuth.class, InternalAuthProvider.class)
// Declares dependencies
.add(Dependency.required(FirebaseOptions.class))
// Defines a factory
.factory(container -> new FirebaseAuth(container.get(FirebaseOptions.class)))
.eagerInDefaultApp() // alwaysEager() or lazy(), lazy is the default.
.build()