Guice系列之用户指南(八)

原文地址:https://code.google.com/p/google-guice/wiki/BuiltInBindings

Built-in Bindings(内建的绑定):即时在injector内的绑定。例如Loggers等。

贴代码:


1

2

3

4

5

6

7

8

9


import com.google.inject.Guice;

import com.google.inject.Injector;

public class Test {

public static void main(String[] args) {

Injector injector = Guice.createInjector();

System.out.println(injector);

}

}

执行结果:
Injector{bindings=[InstanceBinding{key=Key[type=com.google.inject.Stage, annotation=[none]], source=[unknown source], instance=DEVELOPMENT}, ProviderInstanceBinding{key=Key[type=com.google.inject.Injector, annotation=[none]], source=[unknown source], scope=Scopes.NO_SCOPE, provider=Provider}, ProviderInstanceBinding{key=Key[type=java.util.logging.Logger, annotation=[none]], source=[unknown source], scope=Scopes.NO_SCOPE, provider=Provider}]}

练习代码在这里

时间: 2024-08-31 16:46:44

Guice系列之用户指南(八)的相关文章

Guice系列之用户指南(十)

原文地址:https://code.google.com/p/google-guice/wiki/Scopes Scopes:作用域. 默认情况下,Guice每次在调用时都会返回一个新的实例,这种行为是可以通过作用域配置的,作用域允许复用对象实例.在一个应用服务的生命周期中,对象可能是单例的(@Singleton),也可能是一个回话的(@SessionScoped),也可能是一个请求的(@RequestScoped).Guice在web应用中也包含一个servlet扩展的作用域.自定义作用域可以

Guice系列之用户指南(二)

原文地址:https://code.google.com/p/google-guice/wiki/BindingAnnotations BindingAnnotations(绑定注释):一个类型可能会有多个实现类,在绑定时加上注解,这样可以确定在依赖注入时用的具体是哪个实现类. 具体有2种,一种是自定义注解,另一种是@Named. 先看自定义注解,贴代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2

Guice系列之用户指南(六)

原文地址:https://code.google.com/p/google-guice/wiki/UntargettedBindings UntargettedBindings(没有目标的绑定):就是在module里绑定时不需要明确实现类,结合用@ImplementedBy或者@ProvidedBy实现.区别绑定的注解时要加上实现类的类型,即使是父类型. 贴代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Guice系列之用户指南(四)

原文地址:https://code.google.com/p/google-guice/wiki/ProvidesMethods @Provides Methods(@Provides注解方法):用@Provides来注解方法产生需要的对象. 贴代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

Guice系列之用户指南(九)

原文地址:https://code.google.com/p/google-guice/wiki/JustInTimeBindings JustInTimeBindings(即时绑定):主要是在@ImplementedBy和@ProvidedBy实现. 贴代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 4

Guice系列之用户指南(十一)

原文地址:https://code.google.com/p/google-guice/wiki/Injections Injections(各种注入):常见的有构造函数注入,set方法注入,成员变量注入,前面几篇提到过,就说说没有提到的. Optional Injections(可选择的注入):就是在注入时可以选择使用注入,通过@Inject(optional=true)实现. 1 2 3 4 5 6 7 8 9 public class PayPalCreditCardProcessor i

Guice系列之用户指南(三)

原文地址:https://code.google.com/p/google-guice/wiki/InstanceBindings InstanceBindings(实例绑定):基本类型直接绑定值. 贴代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import com

Guice系列之用户指南(五)

原文地址:https://code.google.com/p/google-guice/wiki/ProviderBindings ProviderBindings(提供者绑定):就是实现Provider接口来生产依赖的对象. 贴代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

Guice系列之用户指南(七)

原文地址:https://code.google.com/p/google-guice/wiki/ToConstructorBindings Constructor Bindings(构造器绑定):在父类型上绑定子类实现的构造函数. 贴代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46