说我正在使用春天,我有以下策略......
接口
public interface MealStrategy {
cook(Meat meat);
}
第一战略
@Component
public class BurgerStrategy implements
MealStrategy {
@Autowired CookerDao cookeryDao;
@Override
public void cook(Meat meat) {
cookeryDao.getBurger(meat);
}
}
下一步策略......
@Component
public class SausageStrategy implements
MealStrategy {
@Autowired CookerDao cookeryDao;
@Override
public cook(Meat meat) {
return cookeryDao.getSausage(meat);
}
}
...上下文
@Component
@Scope("prototype")
public class MealContext {
private MealStrategy mealStrategy;
public void setMealStrategy(MealStrategy strategy) {
this.strategy = strategy;
}
public void cookMeal(Meat meat) {
mealStrategy.cook;
}
}
现在说这个上下文是通过mvc控制器访问的,比如......
@Autowired
private MealContext mealContext;
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody Something makeMeal(Meat meat) {
mealContext.setMealStrategy(new BurgerStrategy())
mealContext.cookMeal(meat);
}
上下文应该是一个组件吗?当我这样做时,我得到一个错误,说loadOnStartup是一个nonUniqueBean,策略可能正如你所期望的那样。所有的bean都需要像上面这样的组件,还是我的注释不正确?
我最大的疑问是你能在Spring MVC应用程序中使用这样的上下文吗?我使用@Scope(原型)时遇到的问题是它意味着策略中的cookeryDao调用会返回一个空指针,因为Dao不会被注入。
如何使用弹簧实现上述模式并且还是线程安全的?我正在尝试甚至可能吗?
答案 0 :(得分:44)
由于具体策略通常是在运行时根据提供的参数确定的,所以我建议如下:
@Component
public class BurgerStrategy implements MealStrategy { ... }
@Component
public class SausageStrategy implements MealStrategy { ... }
然后将所有这些策略注入到给定控制器中的地图(以bean名称作为键)并根据请求选择相应的策略。
@Autowired
Map<String, MealStrategy> mealStrategies = new HashMap<>;
@RequestMapping(method=RequestMethod.POST)
public @ResponseBody Something makeMeal(@RequestParam(value="mealStrategyId") String mealStrategyId, Meat meat) {
mealStrategies.get(mealStrategyId).cook(meat);
...
}
答案 1 :(得分:23)
我会使用简单的依赖注入。
@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:
@Autowired
BeanFactory bf;
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody Something makeMeal(Meat meat) {
bf.getBean("burger", MealStrategy.class).cookMeal(meat);
}
您可以选择创建JSR-330限定符而不是文本名称,以便在编译期间捕获拼写错误。
另见:
How to efficiently implement a strategy pattern with spring?