Configure the DI plugin
You can configure the dependency injection (DI) plugin in your application configuration file. These settings affect the behavior of dependency resolution globally and apply to all registered dependencies.
Dependency key mapping
The ktor.di.keyMapping property defines how dependency keys are generalized and matched during resolution. This determines which registered dependencies are considered compatible when resolving a requested type.
The above example matches the default key mapping used by the DI plugin.
Available key mapping options
DefaultUses the default combination:
Supertypes * Nullables * OutTypeArgumentsSupertypes * RawTypesSupertypesAllows resolving a dependency using any of its supertypes.
NullablesAllows matching nullable and non-nullable variants of a type.
OutTypeArgumentsSupertypesAllows covariance on
outtype parameters.RawTypesAllows resolving generic types without considering type arguments.
UnnamedIgnores dependency names (
@Named) when matching.
Combine key mapping options
You can combine key mapping options using set operators * (intersection), + (union) and () (grouping).
In the following example, a dependency registered as List<String> can be resolved as Collection<String> (Supertypes), List or List? (RawTypes and Nullables):
It will not resolve as Collection?, because that combination is not included in the expression.
Conflict resolution policy
The ktor.di.conflictPolicy property controls how the DI container behaves when multiple providers are registered for the same dependency key:
Available policies
DefaultThrows an exception when a conflicting dependency is declared.
OverridePreviousOverrides the previous dependency with the newly provided one.
IgnoreConflictsIn test environments, the DI plugin uses
IgnoreConflictsby default. This allows test code to override production dependencies without triggering errors.