mirror of
https://github.com/google/guice.git
synced 2024-04-21 12:32:36 +00:00
Page:
InjectionPoints
Pages
3rdPartyModules
AOP
AppsThatUseGuice
AssistedInject
Avoid Injecting Closable Resources
AvoidCallingProvideMethodsAndInjectConstructors
AvoidConditionalLogicInModules
AvoidStaticState
BeCarefulAboutIoInProviders
BestPractices
BindingAnnotations
BindingResolution
Bindings
Bootstrap
BoundFields
BuiltInBindings
ClassLoading
CustomInjections
CustomScopes
CyclicDependencies
DocumentPublicBindings
DontReuseAnnotations
Errors
ExtendingGuice
ExtensionSPI
ExternalDocumentation
FrequentlyAskedQuestions
GettingStarted
GoogleAppEngine
Grapher
Guice10
Guice20
Guice30
Guice40
Guice41
Guice42
Guice421
Guice422
Guice423
Guice500
Guice501
Guice510
Guice600
Guice700
GuiceDiscussions
GuiceInKotlin
GuicePersist
GuicePersistMultiModules
Home
InjectOnlyDirectDependencies
InjectingProviders
InjectingTheInjector
InjectionPoints
Injections
InspectingModules
InstanceBindings
JPA
JSR330
JustInTimeBindings
KeepConstructorsHidden
LinkedBindings
MentalModel
MinimizeMutability
ModulesShouldBeFastAndSideEffectFree
Motivation
Multibindings
OSGi
OptionalAOP
OrganizeModulesByFeature
PreferAtProvides
ProviderBindings
ProvidesMethods
RestrictedBindingSource
Scopes
ServletExtensionSPI
ServletModule
ServletRegexKeyMapping
Servlets
SpringComparison
Struts2Integration
ThrowingProviders
ToConstructorBindings
Transactions
UntargettedBindings
UseNullable
Clone
5
InjectionPoints
Copybara-Service edited this page 2020-08-06 10:09:21 -07:00
Injection Points
How Guice decides what gets injected
Constructor Selection
When Guice instantiates a type using its constructor, it decides which constructor to invoke by following these rules:
- Find and return an
@Inject-annotated constructor- If any are
@Inject(optional=true)constructors, record an error. Constructors may not be optional. - If there are multiple
@Inject-annotated constructors, record an error. There may be at most one@Inject-annotated constructor. - If this constructor has binding annotations (like
@Named), record an error. Binding annotations on the constructor's parameters are okay, but not on the constructor itself.
- If any are
- Find and return a no-arguments constructor
- If this constructor is
privatebut the class is notprivate, record an error. Private constructors on non-private classes are not injectable without the@Injectannotation. - If this constructor itself has binding annotations (like
@Named), record an error. Binding annotations on the constructor's parameters are okay, but not on the constructor itself.
- If this constructor is
- No suitable constructor was found. Record an error.
Injecting Fields and Methods
Injections are performed in a specific order. All fields are injected and then all methods. Within the fields, supertype fields are injected before subtype fields. Similarly, supertype methods are injected before subtype methods.
What Gets Injected
Guice injects instance methods and fields of:
- All values that are bound using
toInstance(). These are injected at injector-creation time. - All providers that are bound using
toProvider(Provider). These are injected at injector-creation time. - All instances that are registered for injection using
requestInjection. These are injected at injector-creation time. - The arguments to
Injector.injectMemberswhen that method is invoked. - All values instantiated by Guice via its injectable constructor, immediately after it is instantiated. Regardless of how the value is scoped, it is only injected once.
Guice injects static methods and fields of:
- All classes that are registered for static injection using
requestStaticInjection.
How Guice Injects
To inject a field, Guice will:
- Lookup the binding, creating a just-in-time binding if necessary. If no just-in-time binding can be created, an error is reported.
- The binding is exercised to provide a value. How this works depends on the type of the binding.
- The binding's value is set into the field. Injecting
finalfields is not recommended because the injected value may not be visible to other threads. If the field injection is@Inject(optional=true)and the binding neither exists, nor can be created, the field injection is skipped.
To inject a method, Guice will:
- Lookup bindings for all of the parameters, creating just-in-time bindings if necessary. If any just-in-time binding cannot be created, an error is reported.
- The bindings are each exercised to provide a value. How this works depends on the types of the bindings.
- The method is called, using the bindings' values as arguments. If the method injection is optional and a parameter's binding neither exists nor can be created, the method injection will be skipped.
-
User's Guide
-
Integration
-
Extensions
-
Internals
-
Releases
-
Community