`

guice的单例

阅读更多
关于Guice的单例,自己一直都不是很明白,虽然其用户文档也看过几遍了,可能是本人太愚昧的缘故,一直很迷惑。今天又花了点时间试了一下,基本明白是怎么会事了。

要保证注入的实例是同一个对象,那么第二次注入时就不能再调用构造器了。

实现的方式用几种:

this.bind(Service.class).to(ServiceImp.class).in(Scopes.SINGLETON);
this.bind(Service.class).to(ServiceImp.class).asEagerSingleton() 
this.bind(Service.class).to(ServiceImp.class).in(Singleton.class)


也可以直接在ServiceImp上加@Singleton注解,如果是通过@Provides产生的对象,则可以
用把@Provides @Singleton一起放在其方法上。
文档上说得很清楚:

In linked bindings, scopes apply to the binding source, not the binding target. Suppose we have a class Applebees that implements both Bar and Grill interfaces. These bindings allow for two instances of that type, one for Bars and another for Grills:

bind(Bar.class).to(Applebees.class).in(Singleton.class);
bind(Grill.class).to(Applebees.class).in(Singleton.class);


This is because the scopes apply to the bound type (Bar, Grill), not the type that satisfies that binding (Applebees). To allow only a single instance to be created, use a @Singleton annotation on the declaration for that class. Or add another binding:

bind(Applebees.class).in(Singleton.class);


意思是说提供实例的依据是根据其接口而定的,而不是通过其实现类来定的。
如果要让实现了Bar、Grill的Applebees只提供一个实例(上述情况),可以通过
@Singleton或
 bind(Applebees.class).in(Singleton.class);
来实现。
实现好以后,就可以通过多次注入来检验是否第二次的注入不再调用其构造方法:
ServiceImp类的默认构造器:

public ServiceImp() {
        System.out.println("ServiceImp=============");
    }


测试类代码:

Injector injector = Guice.createInjector(new ServiceModel());
injector.injectMembers(this);
injector.injectMembers(this);

经过测试发现第二次注入并没有再调用其构造方法。

分享到:
评论

相关推荐

    dropwizard-guice-governator:使用带有 dropwizard-guice 的调节器来避免

    当您在注入 dropwizard 配置或环境的类上有一个 guice 单例时,就会发生这种情况。 为避免这种情况,您可以: 将 guice 阶段设置为“DEVELOPMENT”,但所有内容都会以惰性模式加载 使用这样的包

    guice.jar/guice.jar

    guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar

    Guice与Spring框架的区别.pdf

    在这个方法中,我们使用了binder.bind方法将MyService接口绑定到MyServiceImpl实现类,并指定了Scopes.SINGLETON作用域,以确保MyServiceImpl对象是单例的。 Guice的其它使用特性 Guice还提供了一些其它的使用特性...

    Google Guice: Agile Lightweight Dependency Injection Framework

    * What the future has in store, including Guice IDE, the next Guice version and the standardization of Guice's concepts through JSR 299. * How you can build real world, Guice-powered web ...

    guice-3.0-API文档-中英对照版.zip

    赠送jar包:guice-3.0.jar; 赠送原API文档:guice-3.0-javadoc.jar; 赠送源代码:guice-3.0-sources.jar; 赠送Maven依赖信息文件:guice-3.0.pom; 包含翻译后的API文档:guice-3.0-javadoc-API文档-中文(简体)-...

    guice-multibindings-3.0-API文档-中文版.zip

    赠送jar包:guice-multibindings-3.0.jar; 赠送原API文档:guice-multibindings-3.0-javadoc.jar; 赠送源代码:guice-multibindings-3.0-sources.jar; 赠送Maven依赖信息文件:guice-multibindings-3.0.pom; ...

    guice-3.0-API文档-中文版.zip

    赠送jar包:guice-3.0.jar; 赠送原API文档:guice-3.0-javadoc.jar; 赠送源代码:guice-3.0-sources.jar; 赠送Maven依赖信息文件:guice-3.0.pom; 包含翻译后的API文档:guice-3.0-javadoc-API文档-中文(简体)版...

    guice-multibindings-3.0-API文档-中英对照版.zip

    赠送jar包:guice-multibindings-3.0.jar; 赠送原API文档:guice-multibindings-3.0-javadoc.jar; 赠送源代码:guice-multibindings-3.0-sources.jar; 赠送Maven依赖信息文件:guice-multibindings-3.0.pom; ...

    guice入门学习资料

    guice 学习资料,快速掌握guice的编程技巧以及了解其机制。

    guice-assistedinject-3.0-API文档-中英对照版.zip

    赠送jar包:guice-assistedinject-3.0.jar; 赠送原API文档:guice-assistedinject-3.0-javadoc.jar; 赠送源代码:guice-assistedinject-3.0-sources.jar; 赠送Maven依赖信息文件:guice-assistedinject-3.0.pom;...

    guice-4.0-API文档-中文版.zip

    赠送jar包:guice-4.0.jar; 赠送原API文档:guice-4.0-javadoc.jar; 赠送源代码:guice-4.0-sources.jar; 赠送Maven依赖信息文件:guice-4.0.pom; 包含翻译后的API文档:guice-4.0-javadoc-API文档-中文(简体)版...

    google guice基础例子

    Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC)。Guice非常小而且快。Guice是类型安全的,它能够对构造函数,属性,方法(包含任意个参数的任意方法,而不仅仅是setter...

    Guice用户中文指南

    Guice用户中文指南,Guice (读作"juice")是超轻量级的,下一代的,为Java 5及后续版本设计的依赖注入容器

    Guice_1.0_Manual.pdf

    Guice开发手册, Guice中文开发手册

    guice-4.0-API文档-中英对照版.zip

    赠送jar包:guice-4.0.jar; 赠送原API文档:guice-4.0-javadoc.jar; 赠送源代码:guice-4.0-sources.jar; 赠送Maven依赖信息文件:guice-4.0.pom; 包含翻译后的API文档:guice-4.0-javadoc-API文档-中文(简体)-...

    guice-3.0.rar

    guice-3.0,轻量级IOC容器,包含guice-3.0.jar、guice-spring-3.0.jar、guice-struts2-plugin-3.0.jar

    guice-2.0.jar

    guice-2.0.jar guice-2.0.jar

    google guice 3.0源码

    google guice 3.0源码,官方下载,帮助你更好理解google guice实现的原理

    基于guice的简单项目

    基于guice的HelloWorld项目, 内含需要的2个jar包, 直接复制到项目里面就可以用了

Global site tag (gtag.js) - Google Analytics