Package org.int4.dirk.api
Interface InstanceResolver
-
- All Known Subinterfaces:
Injector
- All Known Implementing Classes:
StandardInjector
public interface InstanceResolverProvides methods to resolveTypes to instances.All methods support filtering by qualifier annotation, by providing either an
Annotationinstance (obtainable viaAnnotations#of(Class)) or by providing aClassinstance of <? extends Annotation>. Annotations should be qualifier annotations or no matches will be found.Methods that can return multiple instances also support a
PredicateofTypeto allow custom filtering.Filtering by generic type is possible by providing
ParameterizedTypeor aWildcardType. There are various ways to construct such types, see for exampleorg.int4.dirk.util.Typesandorg.int4.dirk.util.TypeReference.Examples:
getInstance(Database.class)
would return aDatabaseinstance.getInstance(Types.wildcardExtends(Database.class, Queryable.class))
would return an object which implements or extends bothDatabaseandQueryable.getInstances(Vehicle.class, Red.class)
orgetInstances(Vehicle.class, Annotations.of(Red.class))
would return allVehicles instances annotated with the@Redqualifier annotation.getInstance(String.class, Annotations.of(Named.class, Map.of("value", "config.url"))would return aStringinstance which was registered with aNamedannotation with value "config.url".
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T> TgetInstance(java.lang.Class<T> cls, java.lang.Object... qualifiers)Returns an instance of the given class matching the given criteria (if any) in which all dependencies are injected.<T> TgetInstance(java.lang.reflect.Type type, java.lang.Object... qualifiers)Returns an instance of the givenTypematching the given criteria (if any) in which all dependencies are injected.<T> java.util.List<T>getInstances(java.lang.Class<T> cls, java.lang.Object... qualifiers)Returns all instances of the given class matching the given criteria (if any) in which all dependencies are injected.<T> java.util.List<T>getInstances(java.lang.reflect.Type type, java.lang.Object... qualifiers)Returns all instances of the givenTypematching the given criteria (if any) in which all dependencies are injected.
-
-
-
Method Detail
-
getInstance
<T> T getInstance(java.lang.reflect.Type type, java.lang.Object... qualifiers) throws UnsatisfiedResolutionException, AmbiguousResolutionException, CreationException, ScopeNotActiveExceptionReturns an instance of the givenTypematching the given criteria (if any) in which all dependencies are injected. The instance returned can either be an existing instance or newly created depending on its scope.- Type Parameters:
T- the type of the instance- Parameters:
type- the type of the instance required, cannot benullqualifiers- optional list of qualifier annotations, eitherAnnotationorClass<? extends Annotation>- Returns:
- an instance of the given class matching the given criteria, never
null - Throws:
UnsatisfiedResolutionException- when no matching instance was available or could be createdAmbiguousResolutionException- when multiple matching instances were availableCreationException- when an error occurred during creation of a matching instanceScopeNotActiveException- when the scope for the produced type is not active
-
getInstance
<T> T getInstance(java.lang.Class<T> cls, java.lang.Object... qualifiers) throws UnsatisfiedResolutionException, AmbiguousResolutionException, CreationException, ScopeNotActiveExceptionReturns an instance of the given class matching the given criteria (if any) in which all dependencies are injected. The instance returned can either be an existing instance or newly created depending on its scope.- Type Parameters:
T- the type of the instance- Parameters:
cls- the class of the instance required, cannot benullqualifiers- optional list of qualifier annotations, eitherAnnotationorClass<? extends Annotation>- Returns:
- an instance of the given class matching the given criteria (if any)
- Throws:
UnsatisfiedResolutionException- when no matching instance was available or could be createdAmbiguousResolutionException- when multiple matching instances were availableCreationException- when an error occurred during creation of a matching instanceScopeNotActiveException- when the scope for the produced type is not active
-
getInstances
<T> java.util.List<T> getInstances(java.lang.reflect.Type type, java.lang.Object... qualifiers) throws CreationExceptionReturns all instances of the givenTypematching the given criteria (if any) in which all dependencies are injected. When there are no matches, an empty set is returned. The instances returned can either be existing instances or newly created depending on their scope or a mix thereof.- Type Parameters:
T- the type of the instances- Parameters:
type- theTypeof the instances required, cannot benullqualifiers- optional list of qualifier annotations, eitherAnnotationorClass<? extends Annotation>- Returns:
- all instances of the given
Typematching the given criteria (if any), nevernull, can be empty - Throws:
CreationException- when an error occurred during creation of a matching instance
-
getInstances
<T> java.util.List<T> getInstances(java.lang.Class<T> cls, java.lang.Object... qualifiers) throws CreationExceptionReturns all instances of the given class matching the given criteria (if any) in which all dependencies are injected. When there are no matches, an empty set is returned. The instances returned can either be existing instances or newly created depending on their scope or a mix thereof.- Type Parameters:
T- the type of the instances- Parameters:
cls- the class of the instances required, cannot benullqualifiers- optional list of qualifier annotations, eitherAnnotationorClass<? extends Annotation>- Returns:
- all instances of the given class matching the given criteria (if any), never
null, can be empty - Throws:
CreationException- when an error occurred during creation of a matching instance
-
-