Google Guice 是輕量級依賴注入 (DI) 框架,榮獲 Jolt 大獎。相較 Spring,Guice 無 XML 設定、純 Java 程式碼定義綁定、啟動更快。本文示範 Guice 核心運作與 Struts 2 整合完整流程。
com.google.inject.Module 介面,在 configure(Binder) 方法中定義介面與實作類別綁定關係Guice.createInjector(module) 建立,負責物件圖構建與依賴注入Guice 核心:
Web/Struts 2 整合:
Struts 2 核心:
com.greysh.guice
├── action
│ └── UserAction.java
├── config
│ └── ModuleConfig.java
├── model
│ └── User.java
└── service
├── UserService.java
└── impl
└── UserServiceImpl.java
package com.greysh.guice.model;
public class User {
private String userName;
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
}
package com.greysh.guice.service;
import com.greysh.guice.model.User;
public interface UserService {
void invoke(User user);
}
package com.greysh.guice.service.impl;
import com.greysh.guice.model.User;
import com.greysh.guice.service.UserService;
public class UserServiceImpl implements UserService {
public void invoke(User user) {
user.setUserName("Name : " + user.getUserName());
}
}
package com.greysh.guice.config;
import com.google.inject.*;
import com.greysh.guice.service.*;
import com.greysh.guice.service.impl.*;
import com.greysh.guice.model.User;
import com.greysh.guice.action.UserAction;
public class ModuleConfig implements Module {
public void configure(Binder binder) {
// 介面綁定實作,單例範圍
binder.bind(UserService.class).to(UserServiceImpl.class).in(Scopes.SINGLETON);
// 實體類別也納入管理
binder.bind(User.class).in(Scopes.SINGLETON);
// Action 類別綁定
binder.bind(UserAction.class).in(Scopes.SINGLETON);
}
}
package com.greysh.guice.action;
import com.google.inject.Inject;
import com.greysh.guice.model.User;
import com.greysh.guice.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
@Inject private UserService userService;
@Inject private User user;
public String execute() {
userService.invoke(user);
return SUCCESS;
}
// Getter/Setter 供 OGNL 存取
public User getUser() { return user; }
public void setUser(User user) { this.user = user; }
}
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"https://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.objectFactory" value="guice" />
<constant name="struts.i18n.encoding" value="utf8" />
<constant name="guice.module" value="com.greysh.guice.config.ModuleConfig" />
<package name="default" extends="struts-default">
<action name="userAction" class="com.greysh.guice.action.UserAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
struts.objectFactory=guice:指定物件工廠為 Guice,取代 Struts 內建 DIguice.module:指定 Module 實作類別全名struts.i18n.encoding:編碼設定| 面向 | Spring | Guice |
|---|---|---|
| 設定方式 | XML + 註解 | 純 Java 程式碼 (Module) |
| 注入觸發 | 容器啟動時完整注入 | 使用時即時注入 (Lazy) |
| 類別侵入性 | 低 (非侵入) | 高 (@Inject 標註業務碼) |
| 動態綁定 | 困難 (XML 靜態) | 原生支援 (程式碼邏輯) |
| 生態系 | 龐大 (ORM, AOP, MVC, Security...) | 專注 DI、AOP、Servlet 擴充 |
| 學習曲線 | 平緩 | 陡峭 (需懂泛型、註解、Module 模式) |
Guice 以「程式碼即設定」哲學,提供極致輕量的 DI 體驗。整合 Struts 2 僅需三個常數設定與 Module 類別,即可享受型別安全、重構友善、啟動極快的依賴管理。選擇 Guice 或 Spring,取決於專案規模、團隊偏好與生態系需求——適合的才是最好的。