<b>Google Guice模范讲授之操纵入门</b>[Java编程]
本文“<b>Google Guice模范讲授之操纵入门</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
本文通过典范简单地介绍Google Guice的利用,通过下面的典范我们可以知道,Google Guice的利用非常简单.
Google Guice需求利用JDK1.5以上java环境.
下载Google Guice之后,
有以下几个文件:
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文件,将其加入到class path中.
下面简单地介绍典范:
典范1:利用com.谷歌.inject.Module接口实现类
文件名 | 阐明 |
HelloGuice.java | 业务逻辑接口定义文件 |
HelloGuiceImpl.java | 业务逻辑接口实现文件 |
HelloGuiceModule.java | 该文件必须实现com.谷歌.inject.Module接口 |
TestGuice.java | 测试文件 |
HelloGuice.java
view plaincopy to clipboardprint?
package com.test.guice;
public interface HelloGuice {
public void sayHello();
}
package com.test.guice;
public interface HelloGuice {
public void sayHello();
}
HelloGuiceImpl.java
view plaincopy to clipboardprint?
package com.test.guice;
public class HelloGuiceImpl implements HelloGuice {
public void sayHello() {
System.out.println("Hello Guice!");
}
}
package com.test.guice;
public class HelloGuiceImpl implements HelloGuice {
public void sayHello() {
System.out.println("Hello Guice!");
}
}
HelloGuiceModule.java
view plaincopy to clipboardprint?
package com.test.guice;
import com.谷歌.inject.Binder;
import com.谷歌.inject.Module;
public class HelloGuiceModule implements Module {
public void configure(Binder binder) {
binder.bind(HelloGuice.class).to(HelloGuiceImpl.class);
}
}
package com.test.guice;
import com.谷歌.inject.Binder;
import com.谷歌.inject.Module;
public class HelloGuiceModule implements Module {
public void configure(Binder binder) {
binder.bind(HelloGuice.class).to(HelloGuiceImpl.class);
}
}
TestGuice.java
view plaincopy to clipboardprint?
package com.test.guice;
import junit.framework.TestCase;
import com.谷歌.inject.Guice;
import com.谷歌.inject.Injector;
public class TestGuice extends TestCase {
public void testHelloGuice() {
Injector injector = Guice.createInjector(new HelloGuiceModule());
HelloGuice helloGuice = injector.getInstance(HelloGuice.class);
helloGuice.sayHello();
}
}
package com.test.guice;
import junit.framework.TestCase;
import com.谷歌.inject.Guice;
import com.谷歌.inject.Injector;
public class TestGuice extends TestCase {
public void testHelloGuice() {
Injector injector = Guice.createInjector(new HelloGuiceModule());
HelloGuice helloGuice = injector.getInstance(HelloGuice.class);
helloGuice.sayHello();
}
}
以上是“<b>Google Guice模范讲授之操纵入门</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |