Personally I don`t like having a single 'import org.springframework' in my objects (only the ones that where design for Spring).
So.. here is what I would do:
I would create a CommandFactory that is parametrized in Spring (so it has all the dependencies a command should need). And the CommandFactory returns a Command object based on the inputArguments.
Example:
Code:
interface Command{
void execute();
}
class SillyCommand implements Command{
private int _sillyValue;
SillyCommand(int sillyValue, Object data){
_sillyValye = sillyValue;
_data = data;
}
void execute(){
System.out.println(_sillyValue);
}
}
interface CommandFactory{
Command create(String s, .....);
}
class DefaultCommandFactory implements CommandFactory{
private int _sillyValue;
public DefaultCommandFactory(int sillyValue){
_sillyValue = sillyValue;
}
Command create(String s, Object arg){
if("silly".equals(s))
return new SillyCommand(_sillyValye,arg);
... more checks
throw new IllegalArgumentException("unknown command");
}
}
And you can glue it together in Spring:
Code:
<bean id="commandFactory" class="DefaultCommandFactory">
<constructor-arg value="10"/>
</bean>
<bean id="anObjectThatNeedsCommands" class="Foo">
<constructor-arg ref="commandFactory"/>
</bean>
[edit]
And if you would like to create a ConcreteCommandFactory for every Command class if you want and connect them in the the 'GeneralCommandFactory'. But I`m not going to type that example right now