Kinds of Dependencies
Sting allows a component to declare different kinds of dependencies. The kind determines the manner in which
Sting supplies the dependency to the component. The kind is specified by the java type of the constructor
parameter (for Injectable
annotated types), the method parameter (for methods contained in
Fragment
annotated types) or the method return value (for injector outputs).
The kind can also be determined by whether the element has been annotated by the @javax.annotation.Nullable
annotation.
The following table lists the different kinds supported by Sting.
Kind | Java Type | Description |
---|---|---|
Instance | T |
Sting provides a single component that publishes the type T . The Sting annotation
processor will generate an error if there are multiple components that publishes a service of
type T as it is unable to determine which one needs to be supplied to satisfy the
dependency.
|
Nullable Instance | @Nullable T |
Sting provides a single component that publishes the type T if the instance is
included in the injector either via auto-discovery or via explicit includes. If no such component
is present then sting will supply a null value.
|
Supplier | Supplier<T> |
Sting provides a supplier instance that can be invoked to return a single component that publishes
the type T. As with the "Instance" kind, the Sting annotation processor will generate
an error if there are multiple components that publishes a service of type T . Sting
requires that the component not invoke the supplier to access the dependency until the injector is
completely initialized otherwise errors may be generated. It is likely that this will be enforcable
in a later version of Sting. A supplier dependency means the Sting annotation processor need not
order the dependency prior to the consumer component and will not reject the dependency due to it
forming a dependency loop.
|
Collection | Collection<T> |
Sting provides a read-only collection containing every component that publishes
the type T . This is primarily aimed at systems that support multiple
plugins of the same type.
|
Supplier Collection | Collection<Supplier<T>> |
Sting provides a read-only collection containing a supplier for every component that publishes
the type T . This is primarily aimed at systems that support multiple
plugins of the same type and either the plugins can be lazily created when required
or a plugin may have a direct dependency on the component declaring the dependency.
|
Examples
The ErrorResponder
includes an "instance" dependency on the NotificationService
service so that
the component can provide feedback to the user when an error occurs. It also includes a "nullable instance"
dependency on the FaultAuditService
which it will use to report the error to a fault monitoring service.
In local and non-production environments, the injector will not bind a component that publishes the
FaultAuditService
and the injector will pass a null value into the component.
@Injectable
public class ErrorResponder
{
ErrorResponder( NotificationService notificationService,
@Nullable FaultAuditService faultAuditService )
{
...
}
...
}
The ImageConverter
just shows collection dependencies as it assumes the component is capable of
reading in one image format and writing out another image format.
@Injectable
public class ImageConverter
{
ImageConverter( Collection<ImageReader> readers,
Collection<Supplier<ImageWriter>> writers )
{
...
}
...
}
The ImageConverter
component could have easily be returned by a provider method defined in a
fragment such as:
@Fragment
public interface ImageToolsFragment
{
default ImageConverter provideImageConverter( Collection<ImageReader> readers,
Collection<Supplier<ImageWriter>> writers )
{
return new ImageConverter( readers, writers );
}
...
}
The service could also be retrieved as outputs using different dependency kinds:
@Injector
public interface MyInjector
{
Supplier<NotificationService> getNotificationService();
@Nullable
FaultAuditService getFaultAuditService();
...
}