|
Generated by JDiff |
||||||||
PREV PACKAGE NEXT PACKAGE FRAMES NO FRAMES |
This file contains all the changes in documentation in the packagecom.google.inject.assistedinject
as colored differences. Deletions are shownlike this, and additions are shown like this.
If no deletions or additions are shown in an entry, the HTML tags will be what has changed. The new HTML tags are shown in the differences. If no documentation existed, and then some was added in a later version, this change is noted in the appropriate class pages of differences, but the change is not shown on this page. Only changes in existing text are shown here. Similarly, documentation which was inherited from another class or interface is not shown here.
Note that an HTML error in the new documentation may cause the display of other documentation changes to be presented incorrectly. For instance, failure to close a <code> tag will cause all subsequent paragraphs to be displayed differently.
ConstructorsWhen used in tandem with FactoryModuleBuilder, constructors annotated with {@code @AssistedInject} indicate thattheymultiple constructors can beinstantiatedinjected,by theeach with different parameters. AssistedInject annotations should not be mixed with {@literal @}FactoryProviderInject annotations.Each constructorThe assisted parameters must exactly match one corresponding factorymethod withinmethod within the factory interface.,butConstructorthe parametersmust be either supplied bydo not need to be in thefactorysameinterfaceorder.and marked withConstructors annotated with@Assisted,AssistedInjectorarethey must becreated by Guiceinjectableand receive all the benefits (such as AOP).@deprecatedObsolete Usage: When used in tandem with FactoryProvider
now,works betterconstructors annotated withthe standard{@literalcode @InjectAssistedInject}trigger a "backwards compatibilityannotationmode".When using thatThe assisted parametersannotation,mustparametersexactlyarematch onematched by namecorresponding factory methodandwithin the factory interfacetype rather thanand all must be in the samebyorderpositionas listed in the factory. Inadditionthis backwards compatable mode,valuesconstructorsthatannotated with AssistedInject are not createduseby Guice and thus receive none of thestandardbenefits.{@literal@Inject}Constructor
constructorparameters must be either suppliedannotation are eligible forby the factory interfacemethodand marked with@Assisted
, or they mustinterceptionbe injectable.@author jmourits@google.com (Jerome Mourits) @author jessewilson@google.com (Jesse Wilson)
Obsolete. Prefer FactoryModuleBuilder 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
{@code constructedType} is a concrete class with an {@literal @}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 {@literal @}Assisted annotation. This serves to document that the parameter is not bound by your application's modules.public class RealPayment implements Payment { {@literal @}Inject public RealPayment( CreditService creditService, AuthService authService, {@literal @}Assisted Date startDate, {@literal @}Assisted Money amount) { ... } }Any parameter that permits a null value should also be annotated {@code @Nullable}.Configuring factories
In your module, 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 { {@literal @}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 {@literal @}Assisted annotation to disambiguate the parameters. The names must be applied to the factory method's parameters:public interface PaymentFactory { Payment create( {@literal @}Assisted("startDate") Date startDate, {@literal @}Assisted("dueDate") Date dueDate, Money amount); }...and to the concrete type's constructor parameters:public class RealPayment implements Payment { {@literal @}Inject public RealPayment( CreditService creditService, AuthService authService, {@literal @}Assisted("startDate") Date startDate, {@literal @}Assisted("dueDate") Date dueDate, {@literal @}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, {@literal @}{@literal Inject} members will be injected before they are returned.Backwards compatibility using {@literal @}AssistedInject
Instead of the {@literal @}Inject annotation, you may annotate the constructed classes with {@literal @}AssistedInject. This triggers a limited backwards-compatability 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 {@literal @}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. @param
The factory interface @author jmourits@google.com (Jerome Mourits) @author jessewilson@google.com (Jesse Wilson) @author dtm@google.com (Daniel Martin) @deprecated use FactoryModuleBuilder instead.