本文通過范例簡單地介紹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.google.inject.Module接口實現類
文件名 說明 HelloGuice.java 業務邏輯接口定義文件 HelloGuiceImpl.java 業務邏輯接口實現文件 HelloGuiceModule.java 該文件必須實現com.google.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.google.inject.Binder;
import com.google.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.google.inject.Binder;
import com.google.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.google.inject.Guice;
import com.google.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.google.inject.Guice;
import com.google.inject.Injector;
public class TestGuice extends TestCase {
public void testHelloGuice() {
Injector injector = Guice.createInjector(new HelloGuiceModule());
HelloGuice helloGuice = injector.getInstance(HelloGuice.class);
helloGuice.sayHello();
}
}
運行TestGuice,打印出:
Hello Guice!
范例2:使用Java Annotation
我們也可以直接為HelloGuice加上@ImplementedBy注釋,而省略掉對com.google.inject.Module的實現。
HelloGuice.java
view plaincopy to clipboardprint?
package com.test.guice;
import com.google.inject.ImplementedBy;
@ImplementedBy(HelloGuiceImpl.class)
public interface HelloGuice {
public void sayHello();
}
package com.test.guice;
import com.google.inject.ImplementedBy;
@ImplementedBy(HelloGuiceImpl.class)
public interface HelloGuice {
public void sayHello();
}
TestGuice.java
view plaincopy to clipboardprint?
package com.test.guice;
import junit.framework.TestCase;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class TestGuice extends TestCase {
public void testHelloGuice() {
//Injector injector = Guice.createInjector(new HelloGuiceModule());
Injector injector = Guice.createInjector();
HelloGuice helloGuice = injector.getInstance(HelloGuice.class);
helloGuice.sayHello();
}
}
package com.test.guice;
import junit.framework.TestCase;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class TestGuice extends TestCase {
public void testHelloGuice() {
//Injector injector = Guice.createInjector(new HelloGuiceModule());
Injector injector = Guice.createInjector();
HelloGuice helloGuice = injector.getInstance(HelloGuice.class);
helloGuice.sayHello();
}
}
HelloGuiceModule.java不再需要。其余的文件內容不變。
運行TestGuice,打印出:
Hello Guice!
本文通過范例簡單地介紹了Google Guice的使用。至於Google Guice與Spring,Struts等的集成,本文未加涉及,請參考其他文章。