Strategy pattern with spring beans

Strategy pattern with spring beans,第1张

Strategy pattern with spring beans

我将使用简单的依赖注入。

@Component("burger")public class BurgerStrategy implements MealStrategy { ... }@Component("sausage")public class SausageStrategy implements MealStrategy { ... }

Controller

选项A:

@Resource(name = "burger")MealStrategy burger;@Resource(name = "sausage")MealStrategy sausage;@RequestMapping(method = RequestMethod.POST)public @ResponseBody Something makeMeal(Meat meat) {    burger.cookMeal(meat);}

选项B:

@AutowiredBeanFactory bf;@RequestMapping(method = RequestMethod.POST)public @ResponseBody Something makeMeal(Meat meat) {    bf.getBean("burger", MealStrategy.class).cookMeal(meat);}

你可以选择创建JSR-330限定词而不是文本名称,以在编译时捕获拼写错误。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5021847.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存