`
lee3836
  • 浏览: 67448 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

guice入门教程helloworld篇

阅读更多
guice据称比spring运行快100倍,我觉得网上是在瞎忽悠的。guice的实现是借助jdk1.5的Annotation特性实现的,如果你的jdk还是1.4请不要用哦

下面带大家一起helloworld。
新建一个web工程(就是为了方便jar的部署):guiceDemo
引入下面的jar文件

aopalliance.jar

  guice-1.0.jar

  guice-servlet-1.0.jar

  guice-spring-1.0.jar

  guice-struts2-plugin-1.0.jar


本类只用到guice-1.0.jar当然,为了测试要引入junit.jar

现在开始写第一种实现,用Module实现。

创建接口HelloGuice.java
package org.lee.guice.module.demo;

public interface HelloGuice {
 public void sayHello();[/color]
}


接口实现类HelloGuiceImpl.java
package org.lee.guice.module.demo;

public class HelloGuiceImpl implements HelloGuice {
	public void sayHello() {
		System.out.println("hello word");
		
	}

}


创建HelloGuiceModule.java(这里我告诉大家,因为它比spring快100倍其实就是说ioc。这个文件替代了spring的配置文件)
package org.lee.guice.module.demo;

import com.google.inject.Binder;
import com.google.inject.Module;

public class HelloGuiceModule implements Module{

	@Override
	public void configure(Binder binder) {
		binder.bind(HelloGuice.class).to(HelloGuiceImpl.class);
		
	}

}


测试类TestGuice.java
package org.lee.guice.module.demo;

import com.google.inject.Guice;
import com.google.inject.Injector;

import junit.framework.TestCase;

public class TestGuice extends  TestCase{
	public void testHelloGuice(){
		Injector injector = Guice.createInjector(new HelloGuiceModule());
		HelloGuice helloGuice = injector.getInstance(HelloGuice.class);
		helloGuice.sayHello();
	}

}


运行结果helloword。
我在测试的是很发现他很慢,当然比spring快多了
用注解的方式注入,发现不是一般的快,那是相当的快
第二种实现:
业务接口
package org.lee.guice.Annotation.demo;

import com.google.inject.ImplementedBy;

@ImplementedBy(HelloGuiceImpl.class)
public interface HelloGuice {
 public void sayHello();
}

业务接口实现:
package org.lee.guice.Annotation.demo;

public class HelloGuiceImpl implements HelloGuice {

	@Override
	public void sayHello() {
		System.out.println("hello word");
		
	}

}

测试
package org.lee.guice.Annotation.demo;

import com.google.inject.Guice;
import com.google.inject.Injector;

import junit.framework.TestCase;

public class TestGuice extends  TestCase{
	public void testHelloGuice(){
	Injector injector = Guice.createInjector();
	HelloGuice  helloGuice = injector.getInstance(HelloGuice.class);
	helloGuice.sayHello();
	}

}


结果测试时helloword
第二种实现和第一种的区别在于注入通过注解如入的
0
2
分享到:
评论
3 楼 gaoyuanyuan121 2011-04-14  
呃,是测试那儿我自己写错了,呵呵,完美运行~
2 楼 gaoyuanyuan121 2011-04-14  
第二个方法的确很简单,但是怎么运行出错,貌似是没有注入成功~~
1 楼 somefuture 2009-11-15  
不错啊,不知道你现在用Guice开发吗?

相关推荐

    guice入门学习资料

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

    基于guice的简单项目

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

    google guice基础例子

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

    guice.jar/guice.jar

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

    Google Guice: Agile Lightweight Dependency Injection Framework

    * How you can build real world, Guice-powered web applications using popular frameworks like Wicket or Struts 2. * How to develop a full stack Guice / Struts 2 / Hibernate application. * What you ...

    guice 教程

    NULL 博文链接:https://shenbai.iteye.com/blog/1665614

    Guice用户中文指南

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

    Guice_1.0_Manual.pdf

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

    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-2.0.jar

    guice-2.0.jar guice-2.0.jar

    google guice 3.0源码

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

    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-3.0.rar

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

    shiro,guice集成

    shiro,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超轻量级依赖注入

    guice超轻量级依赖注入用了才知道是爽

    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; ...

Global site tag (gtag.js) - Google Analytics