Guice学习之基本概念

Guice学习之基本概念

A DI container should provide:

• An injector, which prepares the object graph to inject the dependencies.
It should take care as what implementation should be injected against
a particular interface reference
. Guice provides
com.google.inject.
Injector
to address this problem. It builds the object graph, tracks
the dependencies for each type, and forms what is core of a dependency
injection framework.

• A declaration of component dependencies.
A mechanism is required
to provide configuration for the dependencies, which could serve as the
basis for binding. Assuming that two implementations of an interface exist.
In such a case, a binding mechanism is required as what implementation
should be injected to a particular interface reference. Guice provides com.
google.inject.AbstractModule to provide a readable configuration. We
need to simply extend this abstract class and provide configurations in it. The
protected abstract void configure() method is overridden, which is a
place to write configurations referred to as bindings.

A dependent consumer. Guice provides @Inject annotation to indicate that
a consumer is dependent on a particular dependency. Injector takes care of
initializing this dependency using the object graph.



Meet the injector

The dependency injection starts with separating behavior from dependency
resolution. Dependency injection calls for the dependencies to be passed in rather
than calling the dependencies from factories or invoking constructors directly.
For accomplishing this, an injector comes into picture. An injector object basically
picks up all declared configuration in modules and prepares a graph of objects.

Once request for an instance is made, it does two things:
Resolves the dependencies using configurations, which are called bindings.
Bindings could be explicit or implicit.

Wires the object graph to instantiate the requested object.
Injector could be initialized using any of the four available static APIs in the
Injector class.
The preferred API is the one, which we use. It basically accepts the variable number
of arguments of com.google.inject.Module type. Here, we could pass all the
instances of various modules, which have binding configurations.

Injector injector = Guice.createInjector(…);

Various kinds of injections

There are three ways of injecting dependencies:
• Field injection
• Method injection
• Constructor injection

Field injection

A field if annotated with @Inject annotation gets dependency injected in it. The member
variables often have accessibility as private, which makes them non-testable, so
it is not a preferred way.


Method injection

In case we need to have our fields properly testable, we usually declare them as
private, along with getter and setter APIs. We could inject the dependency by applying
the @Inject annotation over the setter API, which is in fact a suggested way.


@Inject
public void setFlightSupplier (FlightSupplier flightSupplier) {
this.flightSupplier = flightSupplier;
}


This way of injecting dependencies is termed as method injection.

Constructor injection

Constructor injection is a way to combine instantiation of objects with dependency
injection. Assume a case where in the constructor for FlightEngine is changed and
FlightSupplier needs to be supplied as an argument to the constructor.
In such a case, all we need to do is to annotate the FlightEngine constructor with @
Inject annotation. In this way the injector while instantiation of the FlightEngine
instance, injects the dependency directly.

@Inject
public FlightEngine(CSVSupplier flightSupplier) {
this.flightSupplier = flightSupplier;
}

Configuring a module to write bindings

There are two ways by which we can configure the bindings:
• Firstly by extending the abstract class com.google.inject.AbstractModule
@Override
protected void configure() {
}

• Secondly by implementing the interface com.google.inject.Module
@Override
public void configure(Binder binder) {
}


It is suggested that modules must be extended using
AbstractModule. It provides a more readable configuration
and also avoids repletion of invoking methods on binder.


读书笔记:Learning Google Guice

你可能感兴趣的:(Guice学习之基本概念)