In my current project I'm dealing with EJBs implementing huge interfaces. Implementation is done through a business delegate, which implement the same interface and contains the real business code.
As suggested by some articles like
- http://code.google.com/intl/fr/events/io/2009/sessions/GoogleWebToolkitBestPractices.html
- http://www.nofluffjuststuff.com/conference/boston/2008/04/session?id=10150
The usage sequence of this 'command pattern' is
- client creates a Command and parameterize it
- client send the Command to the server
- server receive command, log, audit and assert command can be served
- server execute command
- server return command result to client
The problem take place in step 4.:
Right now I'm using the spring context to get bean from context inside the command, but I want to inject dependencies into command.
Here is a naive usage for illustration purpose. I've added comments where I have problems:
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();
}
}
I want to get rid of calls like SandwichService sandwichService = SpringServerContext.getBean("sandwichService");
and have my service injected instead.
How to do that the easiest way ?