My favorites | Sign in
Google
                
Search
for
Updated Aug 26, 2009 by limpbizkit
InjectionPoints  
How Guice decides what gets injected

Injection Points

Constructor Selection

When Guice instantiates a type using its constructor, it decides which constructor to invoke by following these rules:

  1. Find and return an @Inject-annotated constructor
  2. Find and return a no-arguments constructor
  3. 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:

Guice injects static methods and fields of:

How Guice Injects

To inject a field, Guice will:

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.


Sign in to add a comment