@Retention(value=RUNTIME)
@Target(value=TYPE)
@Documented
public static @interface Component.Factory
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:
abstract
module that has non-static
binding methods, unless Dagger can
instantiate that module with a visible no-argument constructor.
@BindsInstance
. These
parameters bind the instance passed for that parameter within the component. See @BindsInstance
for more information.
abstract
methods, but they are ignored as far as
validation and factory generation are concerned.
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.