Binding ResolutionThe injector's process of resolving an injection request depends on the bindings and the annotations of the types involved. Here's how an injection request is resolved: - Use explicit bindings.
- If the binding links to another, follow this resolution algorithm for that.
- If the binding specifies an instance, return that.
- If the binding specifies a provider, use that.
- Ask a parent injector. If this injector has a parent injector, ask that to resolve the binding. If it succeeds, use that. Otherwise proceed.
- Ask child injectors. If any child injector already has this binding, give up. A blacklist of bindings from child injectors is kept so injectors don't need to maintain references to their child injectors.
- Handle Provider injections. If the type is Provider<T>, resolve T instead, using the same binding annotation, if it exists.
- Convert constants. If there is a constant string bound with the same annotation, and a TypeConverter that supports this type, use the converted String.
- If the dependency has a binding annotation, give up. Guice will not create default bindings for annotated dependencies.
- If the dependency is an array or enum, give up.
- Handle TypeLiteral injections. If the type is TypeLiteral<T>, inject that value, using context for the value of the type parameter.
- Use resolution annotations. If the dependency's type has @ImplementedBy or @ProvidedBy, lookup the binding for the referenced type and use that.
- If the dependency is abstract or a non-static inner class, give up.
- Use a single @Inject or public no-arguments constructor.
- Validate bindings for all dependencies — the constructor's parameters, plus @Inject methods and fields of the type and all supertypes.
- Invoke the constructor.
- Inject all fields. Supertype fields are injected before subtype fields.
- Inject all methods. Supertype methods are injected before subtype methods.
|