由于Spring太过庞大,所以本人也一直都不太喜欢,当guice1出来时,就关注过一阵子,但始终没有行动去试用,听说2.0出来了,最近闲着也没事,就去试用了一下,单从IOC方面来说,做得的确比较出色。
于是做了一个例子,把常用的功能都试了一下,其中需要的JAR包只有两个:
aopalliance.jar
guice-2.0.jar
下面是例子的代码:
public interface Service {
public void sayHello();
}
public class ServiceImp implements Service {
public void sayHello() {
System.out.println("ServiceImp say Hello !");
}
}
然后需要一个Model建立接口与类之间的映射关系,
由于包含的功能比较多,所以代码相对有一点复杂,不过这个类是最重要的
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Provides;
import com.google.inject.name.Names;
import demo.HeroModule;
public class ServiceModel extends AbstractModule {
@Override
protected void configure() {
// 与在Service接口上声明@ImplementedBy(ServiceImp.class)等价
// 手工实例化想绑定的对象
// this.bind(Service.class).toInstance(new ServiceImp());
// this.bind(Service.class).to(ServiceImp.class);
// When your @Provides methods start to grow complex, you may consider
// moving them to a class of their own
this.bind(Service.class).toProvider(ServiceProvider.class);
// this.bind(String.class).annotatedWith(Names.named("username")).toInstance("qiuqiu");
this.bindConstant().annotatedWith(Names.named("username")).to("qiuqiu");
loadProperties();
}
/*
* When you need code to create an object, use an @Provides method. The
* method must be defined within a module, and it must have an @Provides
* annotation. The method's return type is the bound type. Whenever the
* injector needs an instance of that type, it will invoke the method.
*/
@Provides
public User provide() {
System.out.println("++++++++=============");
User user = new User();
user.setUsername("myprovide");
return user;
}
private void loadProperties() {
InputStream stream = this.getClass().getResourceAsStream(
"/app.properties");
Properties appProperties = new Properties();
try {
appProperties.load(stream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Names.bindProperties(this.binder(), appProperties);
}
}
Provider在管理手工创建对象时会比@Provides灵活一些:
import com.google.inject.Provider;
public class ServiceProvider implements Provider<Service> {
@Override
public Service get() {
System.out.println("ServiceProvider==-=-=-=-=_+_+_+_+_+_+_+_+");
// TODO Auto-generated method stub
Service service = new ServiceImp();
return service;
}
}
还有一辅助类:
public class User{
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
还有一属性文件app.properties,很简单:
db.user=mysql
db.pwd=2009
经过上面一系列的准备,就可以进行测试了了:
import org.testng.Assert;
import org.testng.annotations.Test;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.name.Named;
public class Client {
private Service service;
private User user;
private String username;
private String db_user;
private String db_pwd;
@Inject
public void setDB(@Named("db.user") String db_user,
@Named("db.pwd") String db_pwd) {
this.db_user = db_user;
this.db_pwd = db_pwd;
}
@Inject
public void setUsername(@Named("username") String username) {
this.username = username;
}
@Inject
public void setUser(User user) {
this.user = user;
}
@Inject
public void setService(Service service) {
this.service = service;
}
@Test
public void testGuice() {
Injector injector = Guice.createInjector(new ServiceModel());
// 如果不用@Inject注入,则用这种方法
// Service service = injector.getInstance(ServiceImp.class);
injector.injectMembers(this);
service.sayHello();
}
@Test
public void testBindConstant() {
System.out.println("username=" + username);
}
@Test
public void testBindProvides() {
Injector injector = Guice.createInjector(new ServiceModel());
injector.injectMembers(this);
// 必须要调用上面的注入方法
Assert.assertEquals(user.getUsername(), "myprovide");
}
@Test
public void testProperties() {
Injector injector = Guice.createInjector(new ServiceModel());
injector.injectMembers(this);
Assert.assertEquals(db_user, "mysql");
Assert.assertEquals(db_pwd, "2009");
}
}
大体过程就这样,还有一些细小的功能没有仔细去体会,以后有时间再来完善吧!
分享到:
相关推荐
guice 学习资料,快速掌握guice的编程技巧以及了解其机制。
学习ext ext4 extjs4的好程序,学习guice的好程序,学习mybatis的好程序,有很多可以借鉴的地方,jar包列表对应的博客文章中有。
初学mybatis的可以下载学习一下mybatis-guice-3.4.jar
学习ext, guice 的好程序 学习ext4 extjs4, guice 的好程序 mybatis的好程序,可以借鉴很多东西的好程序
学习ext, guice 的好程序 学习ext4 extjs4, guice 的好程序 mybatis的好程序,可以借鉴很多东西的好程序
学习ext, guice 的好程序 学习ext4 extjs4, guice 的好程序 mybatis的好程序,可以借鉴很多东西的好程序
ExtJs 权限管理,权限控制到按钮,Guice 注入,MyBatis3学习,Filter拦截请求。
采用的框架是struts2.1.8,ibatis3,guice2.0,完整的产品模块和权限模块(部分功能)例子。这是smeite.com商城第一版本的部分代码,测试通过。 希望给需要的朋友一个借鉴。 自我认为是学习Ibatis3比较好的例子,覆盖...
简单,快速,基本没有学习成本。 Guice和spring各有所长,Guice更适合与嵌入式或者高性能但项目简单方案,如OSGI容器,spring更适合大型项目组织。 注入方式 在我们谈到IOC框架,首先我们的话题将是构造...
它的目的是创建一个通用项目,该项目可用作新项目的起点,也可用于学习工作(我正在建立一个开放的游戏数据库)。 基于版本 0.7.0 使用依赖注入(没有 Spring 依赖!) / JPA 2.1 作为数据库访问框架 作为数据库...
深入学习依赖注入原理的好书 Dependency Injection is an in-depth guide to the current best practices for using the Dependency Injection pattern-the key concept in Spring and the rapidly-growing Google ...
依赖注入与JSR-330的参考实现——Guice.docx 关于Java框架Vert.x的几点思考.docx 关于堆和栈的那些事.docx 写好Java代码的30条经验总结.docx 华为java笔试面试题2014.doc 多态的理解.docx 大公司最喜欢问的Java集合...
Shiro(Apache Shiro)是一个强大且易于使用的Java安全框架,用于身份验证、授权、...容器友好:Shiro可以与常见的Java容器(如Spring、Guice)以及其他框架(如Apache Struts、Apache Wicket)进行集成,方便开发者
该系统使用了jQuery、jqGrid、jsp、servlet、guice、mybatis和thymeleaf等技术,是一个简单的Java内容管理系统。适合用于学习和实践JavaScript、Java、CSS和HTML技术,以及开发基于Java的简单Java内容管理系统相关的...
Spring的默认play-java模板眼镜 介绍让Play Framework <2.4与Spring Framework一起使用相对... 但是,由于有了Spring-Guice快照项目,我们不必学习和编写复杂的Spring和Guice内部结构。笔记可以在上找到另一个Play
本书由GOOGLE软件工程师编写,不仅介绍了依赖注入相关的技术知识,而且讲解了与之相关的Spring和Guice框架。 适合开始接触和使用DI的技术者以及掌握该技术并拥有相关开发经历的技术人员做进一步提高用。 MANNING...
依赖注入与JSR-330的参考实现——Guice.docx 关于Java框架Vert.x的几点思考.docx 关于堆和栈的那些事.docx 写好Java代码的30条经验总结.docx 华为java笔试面试题2014.doc 多态的理解.docx 大公司最喜欢问的Java集合...
依赖注入与JSR-330的参考实现——Guice.docx 关于Java框架Vert.x的几点思考.docx 关于堆和栈的那些事.docx 写好Java代码的30条经验总结.docx 华为java笔试面试题2014.doc 多态的理解.docx 大公司最喜欢问的Java集合...
然而,随着我尝试学习和练习不同的东西,它随着时间的推移而发展,例如: 使用 SparkJava 框架创建控制器 使用 Google 的 Guice 框架进行依赖注入 使用 Google 的 Gson 轻松序列化/反序列化 Java 对象。 在 Hacker...