Injecting Services into Injector
When creating creating an injector it is common to want to pass services and values into the injector
that are managed externally. You can pass in environment-specific components such as a ServletContext
when running in a servlet container or configuration settings in other contexts.
To implement this in Sting, the developer must specify the required services using the
@Injector.inputs
parameter. The @Injector.inputs
parameter accepts an array of annotations that define the service interface using a type,
qualifier and a flag indicating whether the service is optional.
Sting will generate an injector with a constructor that accepts the input services as parameters in
the order specified and using the types specified. Optional service inputs will also be annotated
with the @Nullable
annotation.
For example, the injector defined by:
@Injector( inputs = { @Injector.Input( type = AuthContext.class ),
@Injector.Input( type = LocalStorage.class, optional = true ),
@Injector.Input( type = String.class, qualifier = "hostname", optional = true ) } )
public interface MyInjector
{
@Nonnull
static MyInjector create( @Nonnull final AuthContext authContext,
@Nullable final LocalStorage localStorage,
@Nullable final String hostname )
{
return new Sting_MyInjector( authContext, localStorage, hostname );
}
...
}
produces an injector implementation with a constructor with the signature:
Sting_MyInjector(@Nonnull final AuthContext input1, @Nullable final LocalStorage input2,
@Nullable final String input3) {