我将使用简单的依赖注入。
@Component("burger")public class BurgerStrategy implements MealStrategy { ... }@Component("sausage")public class SausageStrategy implements MealStrategy { ... }
选项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限定词而不是文本名称,以在编译时捕获拼写错误。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)