java - Strategy Pattern and Dependency Injection in Spring -
i have strategy interface, implemented strategya , strategyb, both of them defined @component's , have @autowired attribute well, how can obtain instance of 1 of them based on string value?
this controller's action, should perform strategy:
@requestmapping("/blabla") public void perform (@requestparam string strategyname) { strategy strategy = (strategy) /* concrete strategy based on strategyname */; strategy.dostuff (); }
thanks!
you can programmatically:
private @autowired beanfactory beanfactory; @requestmapping("/blabla") public void perform (@requestparam string strategyname) { strategy strategy = beanfactory.getbean(strategyname, strategy.class); strategy.dostuff(); }
you fancier way using custom webargumentresolver
, that's lot more trouble it's worth.
Comments
Post a Comment