- package com.leo.service;
-
- import com.google.inject.ImplementedBy;
- import com.leo.service.impl.HelloServiceImpl;
-
-
-
-
-
-
-
-
- @ImplementedBy(HelloServiceImpl.class)
- public interface IHelloService {
- public String sayHello(String str);
- }
package com.leo.service;
import com.google.inject.ImplementedBy;
import com.leo.service.impl.HelloServiceImpl;
/*
* 采用annotation進行接口與實現類之間的綁定
* 注意:接口與實現類之間綁定是必須的,如果只是單獨一個類,沒有接口,
* 那么Guice會隱式的自動幫你注入。并且接口此是不應該聲明注入域范圍的,
* 應該在其實現地方聲明
*
*/
@ImplementedBy(HelloServiceImpl.class)
public interface IHelloService {
public String sayHello(String str);
}
再來一個簡單的實現:
HelloServiceImpl.java
- package com.leo.service.impl;
-
- import com.google.inject.Singleton;
- import com.leo.service.IHelloService;
-
-
-
-
-
-
-
-
-
- @Singleton
- public class HelloServiceImpl implements IHelloService {
-
- public String sayHello(String str) {
- return new StringBuilder("Hello " + str + " !").toString();
- }
-
- }
package com.leo.service.impl;
import com.google.inject.Singleton;
import com.leo.service.IHelloService;
/*
* 這里如果默認不用annotation標注其作用域,或在自定義的module也不指定的話
* 默認的創建對象的方式是類似于Spring的prototype,在此處因為僅僅是一個stateless service
* 我們用@Singleton來標注它,更多的作用域可看Guice文檔
*
* 注意:與標注@Singleton等效的工作也可以在自定義的module里來實現
*/
@Singleton
public class HelloServiceImpl implements IHelloService {
public String sayHello(String str) {
return new StringBuilder("Hello " + str + " !").toString();
}
}
Struts2的配置相信大家都會了,這里需要注意的是Struts2的工廠已經變了,默認是Spring現在我們要改成Guice,請看:
struts.properties
- struts.objectFactory = guice
- #如果自已想實現Module接口,則下面注釋請去掉
- #guice.module=com.leo.module.MyModule
- struts.action.extension=
struts.objectFactory = guice
#如果自已想實現Module接口,則下面注釋請去掉
#guice.module=com.leo.module.MyModule
struts.action.extension=
再來看看調用代碼,稍微比Spring簡潔了些:
HelloAction.java
- package com.leo.action;
-
- import com.google.inject.Inject;
- import com.leo.service.IHelloService;
- import com.opensymphony.xwork2.ActionSupport;
-
- public class HelloAction extends ActionSupport {
-
- private static final long serialVersionUID = -338076402728419581L;
-
-
-
-
- @Inject
- private IHelloService helloService;
-
- private String message;
-
- public String getMessage() {
- return message;
- }
-
- public void setMessage(String message) {
- this.message = message;
- }
-
- public String execute() {
- message = helloService.sayHello("leo");
- return SUCCESS;
- }
-
- }
package com.leo.action;
import com.google.inject.Inject;
import com.leo.service.IHelloService;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
private static final long serialVersionUID = -338076402728419581L;
/*
* 通過field字段進行注入,除此之外,還有construct, method注入均可
*/
@Inject
private IHelloService helloService;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
message = helloService.sayHello("leo");
return SUCCESS;
}
}
struts.xml配置也是非常簡單:
struts.xml
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
- <package name="default" extends="struts-default">
- <action name="hello" class="com.leo.action.HelloAction">
- <result>index.jsp</result>
- </action>
- </package>
- </struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="hello" class="com.leo.action.HelloAction">
<result>index.jsp</result>
</action>
</package>
</struts>
到這里,算是大功告成了,Guice文檔在與Struts2整合部分例子有誤,而且郁悶的是,竟然連Guice的Filter需要在web.xml配置都沒有說,我把配好的web.xml弄出來給大家看看
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
- <filter>
- <filter-name>guice</filter-name>
- <filter-class>
- com.google.inject.servlet.GuiceFilter
- </filter-class>
- </filter>
-
- <filter>
- <filter-name>struts</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.FilterDispatcher
- </filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>guice</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <filter-mapping>
- <filter-name>struts</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>guice</filter-name>
<filter-class>
com.google.inject.servlet.GuiceFilter
</filter-class>
</filter>
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>guice</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
可以布署,運行了,輸入http://localhost:8080/struts2_guice/hello 就可以看到結果了。
如果你覺得Annotation太麻煩,或不喜歡,也可以嘗試自己實現Guice的Module,以下是一個簡單的實現:
MyModule.java
- package com.leo.module;
-
- import com.google.inject.Binder;
- import com.google.inject.Module;
- import com.google.inject.Scopes;
- import com.leo.service.IHelloService;
- import com.leo.service.impl.HelloServiceImpl;
-
-
-
-
-
-
-
- public class MyModule implements Module {
-
- public void configure(Binder binder) {
-
-
-
-
-
-
- binder.bind(IHelloService.class).to(HelloServiceImpl.class).in(
- Scopes.SINGLETON);
- }
-
- }
package com.leo.module;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.leo.service.IHelloService;
import com.leo.service.impl.HelloServiceImpl;
/*
* 如果你覺得Annotation有種支離破碎的感覺,別急,Guice還為你提供一種統一
* 注入管理的自定義實現。在本例中,先前的IHelloService, HelloServiceImpl
* 你現在可以完全將所有的Annotation去掉,然后實現Module接口的唯一一個方法
* 實現如下
*/
public class MyModule implements Module {
public void configure(Binder binder) {
/*
* 將接口IHelloService 與其實現HelloServiceImpl 綁定在一起 并且作用域為Scopes.SINGLETON
* 在這里有多種配置方法,但因為是入門實例,不想說的太復雜。其中如果不配置作用域,默認就是類似于Spring
* 的Scope="prototype"
*/
binder.bind(IHelloService.class).to(HelloServiceImpl.class).in(
Scopes.SINGLETON);
}
}
運行效果完全一模一樣,因此團隊開發如果統一風格的話Guice確實能快速不少。但目前Guice僅僅只是一個IoC,遠遠沒有Spring所涉及的那么廣,但又正如Rod Johnson反復在其《J2EE without EJB》里強調:架構要永遠 simplest, simplest 再 simplest,因此你覺得夠用,就是最好的。
總的來說,開發,運行的速度似乎又快了不少,但Guice真的能不能扛起其所說的下一代IoC容器,我們拭目以待吧。