在我當前的項目中,我正在處理實現巨大接口的EJB。 實現是通過業務委託來完成的,該業務委託實現相同的接口幷包含真實的業務代碼。Java + Command Pattern + Spring + remoting:如何向Command對象注入依賴項?
像
- http://code.google.com/intl/fr/events/io/2009/sessions/GoogleWebToolkitBestPractices.html
- http://www.nofluffjuststuff.com/conference/boston/2008/04/session?id=10150
這個 '命令模式' 的使用順序建議的一些文章是
- 客戶端創建一個命令和參數它
- 客戶端發送命令到服務器
- 服務器接收命令,日誌,審計和斷言命令可以送達
- 服務器上執行命令
- 服務器返回命令結果的客戶
的問題發生在步驟4 .:
現在我正在使用spring context從命令中的上下文獲取bean,但是我想將依賴項注入命令。
這是一個天真的用法說明的目的。我已經添加評論我哪裏有問題:
public class SaladCommand implements Command<Salad> {
String request;
public SaladBarCommand(String request) {this.request = request;}
public Salad execute() {
//this server side service is hidden from client, and I want to inject it instead of retrieving it
SaladBarService saladBarService = SpringServerContext.getBean("saladBarService");
Salad salad = saladBarService.prepareSalad(request);
return salad;
}
}
public class SandwichCommand implements Command<Sandwich> {
String request;
public SandwichCommand(String request) {this.request = request;}
public Sandwich execute() {
//this server side service is hidden from client, and I want to inject it instead of retrieving it
SandwichService sandwichService = SpringServerContext.getBean("sandwichService");
Sandwich sandwich = sandwichService.prepareSandwich(request);
return sandwich;
}
}
public class HungryClient {
public static void main(String[] args) {
RestaurantService restaurantService = SpringClientContext.getBean("restaurantService");
Salad salad = restaurantService.execute(new SaladBarCommand(
"chicken, tomato, cheese"
));
eat(salad);
Sandwich sandwich = restaurantService.execute(new SandwichCommand(
"bacon, lettuce, tomato"
));
eat(sandwich);
}
}
public class RestaurantService {
public <T> execute(Command<T> command) {
return command.execute();
}
}
我想擺脫調用像SandwichService sandwichService = SpringServerContext.getBean("sandwichService");
,並已注入我的服務,而不是。
如何做到最簡單的方法?
這個問題與EJB有什麼關係,真的嗎?看起來你只是問如何在Spring中連接事情。什麼是'SampleCommand'?誰實例化它?誰使用它?它是什麼? – skaffman 2010-01-26 15:24:02
我已經更新了我的問題,以便在skaffman發表評論後作出澄清。 – Guillaume 2010-01-26 16:31:24
如果你曾經爲這個問題實施過某些東西,我很樂意聽到它。我遇到與執行客戶端不需要關心的命令所需的服務端服務相同的問題。 – Jaapjan 2011-10-19 13:57:22