title: Java + Command Pattern + Spring + remoting : How to inject dependencies to Command object?
body: <p>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.</p>
  
  <p>As suggested by some articles like</p>
  
  <ul>
  <li><a href="http://code.google.com/intl/fr/events/io/2009/sessions/GoogleWebToolkitBestPractices.html" rel="nofollow noreferrer">http://code.google.com/intl/fr/events/io/2009/sessions/GoogleWebToolkitBestPractices.html</a> </li>
  <li><a href="http://www.nofluffjuststuff.com/conference/boston/2008/04/session?id=10150" rel="nofollow noreferrer">http://www.nofluffjuststuff.com/conference/boston/2008/04/session?id=10150</a></li>
  </ul>
  
  <p>The usage sequence of this 'command pattern' is </p>
  
  <ol>
  <li>client creates a Command and parameterize it</li>
  <li>client send the Command to the server</li>
  <li>server receive command, log, audit and assert command can be served</li>
  <li>server execute command</li>
  <li>server return command result to client</li>
  </ol>
  
  <p>The problem take place in step 4.:</p>
  
  <p>Right now I'm using the spring context to get bean from context inside the command,
  but I want to inject dependencies into command.</p>
  
  <p>Here is a naive usage for illustration purpose. I've added comments where I have problems:</p>
  
  <pre><code>public class SaladCommand implements Command&lt;Salad&gt; {    
      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&lt;Sandwich&gt; {    
      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 &lt;T&gt; execute(Command&lt;T&gt; command) {
          return command.execute();
      }
  }
  </code></pre>
  
  <p>I want to get rid of calls like <code>SandwichService sandwichService = SpringServerContext.getBean("sandwichService");</code> 
  and have my service injected instead. </p>
  
  <p>How to do that the easiest way ?</p>