Google Guice提供了一个名为@Singleton的注解,只要在类上加上这个注解,就可以实现一个单例类,不需要自己手动编写单例实现类。@Named注解提供了为属性赋值的功能。
一。Scope
package com.template.juice;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-8-5
* Time: 下午8:26
*/
public enum Scope {
REQUEST,
SESSION,
GLOBAL,
UNSCOPE
}
二。Database
package com.template.juice;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-8-5
* Time: 下午8:33
*/
@Singleton
public class Database {
@Named("driverClassName")
@Inject
private String driverClassName;
@Named("url")
@Inject
private String url;
@Named("username")
@Inject
private String username;
@Named("password")
@Inject
private String password;
/**
* Just for test and study,it does not make sense.
*/
@Named("scope")
@Inject
private Scope scope;
public String driverClassName() {
return driverClassName;
}
public String url() {
return url;
}
public String username() {
return username;
}
public String password() {
return password;
}
public Scope scope() {
return scope;
}
}
三。DatabaseModule
package com.template.juice;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-8-5
* Time: 下午8:38
*/
public class DatabaseModule extends AbstractModule {
@Override
protected void configure() {
bind(String.class).annotatedWith(Names.named("driverClassName")).toInstance("com.mysql.jdbc.Driver");
bind(String.class).annotatedWith(Names.named("url")).toInstance("jdbc:mysql://localhost:3306/demo");
bind(String.class).annotatedWith(Names.named("username")).toInstance("root");
bind(String.class).annotatedWith(Names.named("password")).toInstance("root");
bind(Scope.class).annotatedWith(Names.named("scope")).toInstance(Scope.GLOBAL);
}
}
四。Main
package com.template.juice;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by IntelliJ IDEA.
* User: Zhong Gang
* Date: 11-8-5
* Time: 下午8:42
*/
public class Main {
private static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
Module module = new DatabaseModule();
Injector injector = Guice.createInjector(module);
Database instance1 = injector.getInstance(Database.class);
Database instance2 = injector.getInstance(Database.class);
logger.info("driverClassName is " + instance1.driverClassName());
logger.info("url is " + instance1.url());
logger.info("username is " + instance1.username());
logger.info("password is " + instance1.password());
Boolean isSingleton = instance1.equals(instance2);
logger.info(instance1.toString());
logger.info(instance2.toString());
logger.info(isSingleton.toString());
}
}
五。运行结果
可以看到instance1和instance2的hashcode相同,并且两者相比为true,说明injector返回的是同一个实例。
去除Database类的@Singleton注解,运行结果为
可以看到去除@Singleton注解后,instance1和instance2的hashcode不同,并且两者相比为false,说明instance1和instance2为不同的实例。
- 大小: 10.5 KB
- 大小: 10.5 KB
分享到:
相关推荐
Guice中文文档,介绍Guice的基本使用,适合初学者。
Google Guice 这个高效的与Spring类似的依赖注入框架; MyBatis配置和使用; Google Guice与MyBatis集成,支持注解事务,简单的无法想象; Mybatis与mysql集成;实现发送邮件轮询; 源码是个web项目,里面有数据库的...
google guice 3.0源码,官方下载,帮助你更好理解google guice实现的原理
用 Guice 写 Java Guice 1.0 用户指南 王咏刚 译 Guice (读作"juice")是超轻量级的,下一代的,为Java 5及后续版本设计的依赖注入容器。
Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC)。Guice非常小而且快。Guice是类型安全的,它能够对构造函数,属性,方法(包含任意个参数的任意方法,而不仅仅是setter...
谷歌Guice开发英文文档,很详细,对开发很有帮助,可当成工具书使用!
Google Guice需要的jar包: Guice-3.0.jar javax.inject.jar
Google Guice: Agile Lightweight Dependency Injection Framework will not only tell you "how," it will also tell you "why" and "why not," so that all the knowledge you gain will be as widely applicable ...
DI容器,例如spring,picoContainer,EJB容器等等 与Guice的不同
google-guice用户手册,据说和spring pk
使用@Named("foo")注入配置值 使用@Defaults("foo=bar")提供默认值 - 在META-INF/settings处理成文件,以便系统始终可以在没有配置文件的情况下运行 覆盖名为/etc/defaults.properties 、 ~/defaults.properties 、 ...
NULL 博文链接:https://m635674608.iteye.com/blog/2090042
博文链接:https://avengerbevis.iteye.com/blog/69237
guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar
用户指南 博文链接:https://hejianjie.iteye.com/blog/83374
Guice (读作"juice")是超轻量级的,下一代的,为Java 5及后续版本设计的依赖注入容器。 <br>
该项目包含一组Google Guice扩展程序,可用于开发中。 符合OSGi: 生成状态: 问题: : //github.com/mycila/license-maven-plugin/issues 许可证: Apache License 2.0 贡献者 @mgoellnitz @keeganwitt ...
标签:google、inject、guice、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...
标签:google、inject、extensions、guice、multibindings、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变...