私の現在のプロジェクトでは、巨大なインターフェースを実装する EJB を扱っています。実装は、同じインターフェイスを実装し、実際のビジネス コードを含むビジネス デリゲートを通じて行われます。
のようないくつかの記事で示唆されているように
- http://code.google.com/intl/fr/events/io/2009/sessions/GoogleWebToolkitBestPractices.html
- http://www.nofluffjuststuff.com/conference/boston/2008/04/session?id=10150
この「コマンドパターン」の使用順序は、
- クライアントはコマンドを作成し、それをパラメータ化します
- クライアントはコマンドをサーバーに送信します
- サーバー受信コマンド、ログ、監査、およびアサートコマンドを提供できます
- サーバー実行コマンド
- サーバーはコマンドの結果をクライアントに返す
問題はステップ 4 で発生します。
現在、コマンド内のコンテキストから 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");
のような呼び出しを取り除き、代わりにサービスを注入したいと考えています。
それを最も簡単な方法で行う方法は?