Guice 简明教程

Google Guice - Just-In-Time Bindings

在绑定模块中定义绑定信息时,Guice 会在需要注入依赖项时使用这些信息。如果没有绑定信息,它会尝试创建即时绑定信息。在绑定模块中存在的绑定信息称为显式绑定信息,具有更高的优先级,而即时绑定信息称为隐式绑定信息。如果两种类型的信息都存在,则会考虑显式绑定信息进行映射。

As bindings are defined in Binding Module, Guice uses them whenever it needs to inject dependencies. In case bindings are not present, it can attempt to create just-in-time bindings. Bindings present in binding module are called explicit bindings and are of higher precedence whereas just-in-time bindings are called implicit bindings. If both type of bindings are present, explicit bindings are considered for mapping.

以下是三种即时绑定信息的示例。

Following are the examples of three types of Just-in-time bindings.

Binding Type

Description

Injectable Constructors

Non-private, No-argument constructors are eligible for just-in-time bindings. Another way is to annotate a constructor with @Inject annotation.

@ImplementedBy annotation

@ImplementedBy annotation tells the guice about the implementation class. No binding is required in Binding Module in such a case.

@ProvidedBy annotation

@ProvidedBy annotation tells the guice about the provider of implementation class. No binding is required in Binding Module in such a case.