Package com.google.inject.assistedinject
Class FactoryProvider<F>
- java.lang.Object
-
- com.google.inject.assistedinject.FactoryProvider<F>
-
- Type Parameters:
F
- The factory interface
- All Implemented Interfaces:
Provider<F>
,HasDependencies
,jakarta.inject.Provider<F>
,javax.inject.Provider<F>
@Deprecated public class FactoryProvider<F> extends Object implements Provider<F>, HasDependencies
Deprecated.useFactoryModuleBuilder
instead.Obsolete. PreferFactoryModuleBuilder
for its more concise API and additional capability.Provides a factory that combines the caller's arguments with injector-supplied values to construct objects.
Defining a factory
Create an interface whose methods return the constructed type, or any of its supertypes. The method's parameters are the arguments required to build the constructed type.public interface PaymentFactory { Payment create(Date startDate, Money amount); }
You can name your factory methods whatever you like, such as create, createPayment or newPayment.Creating a type that accepts factory parameters
constructedType
is a concrete class with an @Inject
-annotated constructor. In addition to injector-supplied parameters, the constructor should have parameters that match each of the factory method's parameters. Each factory-supplied parameter requires an @Assisted
annotation. This serves to document that the parameter is not bound by your application's modules.public class RealPayment implements Payment { @Inject public RealPayment( CreditService creditService, AuthService authService, @Assisted Date startDate, @Assisted Money amount) { ... } }
Any parameter that permits a null value should also be annotated@Nullable
.Configuring factories
In yourmodule
, bind the factory interface to the returned factory:bind(PaymentFactory.class).toProvider( FactoryProvider.newFactory(PaymentFactory.class, RealPayment.class));
As a side-effect of this binding, Guice will inject the factory to initialize it for use. The factory cannot be used until the injector has been initialized.Using the factory
Inject your factory into your application classes. When you use the factory, your arguments will be combined with values from the injector to construct an instance.public class PaymentAction { @Inject private PaymentFactory paymentFactory; public void doPayment(Money amount) { Payment payment = paymentFactory.create(new Date(), amount); payment.apply(); } }
Making parameter types distinct
The types of the factory method's parameters must be distinct. To use multiple parameters of the same type, use a named @Assisted
annotation to disambiguate the parameters. The names must be applied to the factory method's parameters:public interface PaymentFactory { Payment create( @Assisted("startDate") Date startDate, @Assisted("dueDate") Date dueDate, Money amount); }
...and to the concrete type's constructor parameters:public class RealPayment implements Payment { @Inject public RealPayment( CreditService creditService, AuthService authService, @Assisted("startDate") Date startDate, @Assisted("dueDate") Date dueDate, @Assisted Money amount) { ... } }
Values are created by Guice
Returned factories use child injectors to create values. The values are eligible for method interception. In addition, @Inject members will be injected before they are returned.Backwards compatibility using @AssistedInject
Instead of the @Inject annotation, you may annotate the constructed classes with @AssistedInject
. This triggers a limited backwards-compatibility mode.Instead of matching factory method arguments to constructor parameters using their names, the parameters are matched by their order. The first factory method argument is used for the first @Assisted constructor parameter, etc.. Annotation names have no effect.
Returned values are not created by Guice. These types are not eligible for method interception. They do receive post-construction member injection.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
equals(Object obj)
Deprecated.F
get()
Deprecated.Provides an instance ofT
.Set<Dependency<?>>
getDependencies()
Deprecated.Returns the known dependencies for this type.int
hashCode()
Deprecated.static <F> Provider<F>
newFactory(TypeLiteral<F> factoryType, TypeLiteral<?> implementationType)
Deprecated.static <F> Provider<F>
newFactory(Class<F> factoryType, Class<?> implementationType)
Deprecated.
-
-
-
Method Detail
-
newFactory
public static <F> Provider<F> newFactory(Class<F> factoryType, Class<?> implementationType)
Deprecated.
-
newFactory
public static <F> Provider<F> newFactory(TypeLiteral<F> factoryType, TypeLiteral<?> implementationType)
Deprecated.
-
getDependencies
public Set<Dependency<?>> getDependencies()
Deprecated.Description copied from interface:HasDependencies
Returns the known dependencies for this type. If this has dependencies whose values are not known statically, a dependency for theInjector
will be included in the returned set.- Specified by:
getDependencies
in interfaceHasDependencies
- Returns:
- a possibly empty set
-
get
public F get()
Deprecated.Description copied from interface:Provider
Provides an instance ofT
.
-
-