Package dagger

Annotation Interface Component.Factory

Enclosing class:
Component

@Retention(RUNTIME) @Target(TYPE) @Documented public static @interface Component.Factory
A factory for a component.

A factory is a type with a single method that returns a new component instance each time it is called. The parameters of that method allow the caller to provide the modules, dependencies and bound instances required by the component.

Components may have a single nested static abstract class or interface annotated with @Component.Factory. If they do, then Dagger will generate a factory class that will implement that type. Note that a component with a @Component.Factory may not also have a @Component.Builder.

Factory types must follow some rules:

  • There must be exactly one abstract method, which must return the component type or one of its supertypes.
  • The method must have a parameter for each component dependency.
  • The method must have a parameter for each non-abstract module that has non-static binding methods, unless Dagger can instantiate that module with a visible no-argument constructor.
  • The method may have parameters for modules that Dagger can instantiate or does not need to instantiate.
  • The method may have parameters annotated with @BindsInstance. These parameters bind the instance passed for that parameter within the component. See @BindsInstance for more information.
  • There may be non-abstract methods, but they are ignored as far as validation and factory generation are concerned.
For example, this could be a valid Component with a Factory:

 @Component(modules = {BackendModule.class, FrontendModule.class})
 interface MyComponent {
   MyWidget myWidget();

   @Component.Factory
   interface Factory {
     MyComponent newMyComponent(
         BackendModule bm, FrontendModule fm, @BindsInstance Foo foo);
   }
 }

For a root component, if a @Component.Factory is defined, the generated component type will have a static method named factory() that returns an instance of that factory.

Since:
2.22