春豆战略模式 [英] Strategy pattern with spring beans

查看:93
本文介绍了春豆战略模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我正在使用spring,我有以下策略...

Say I'm using spring, I have the following strategies...

接口

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控制器访问的,例如...

Now say this context was being accessed through an mvc controller, like...

@Autowired
private MealContext mealContext;

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody Something makeMeal(Meat meat) {
    mealContext.setMealStrategy(new BurgerStrategy())
    mealContext.cookMeal(meat);
}

上下文应该是一个组成部分吗?当我收到错误消息说loadOnStartup时,正如您所期望的,该策略可能是一个nonUniqueBean.所有的bean是否都需要像上面一样的组件,或者我的注释不正确?

Should the context be a component? When I do I get an error saying loadOnStartup an there's a nonUniqueBean that the strategy could be, as you'd expect. Do all of the beans need to be components like above or are my annotations incorrect?

我最大的疑问确实是,您可以在Spring MVC应用程序中使用类似的上下文吗?我在使用@Scope(prototype)时也遇到了问题,那就是策略中的cookeryDao调用会返回空指针,因为Dao不会被注入.

My biggest query really is can you use a context like that in a Spring MVC app? The problem I have with using @Scope(prototype) too is it means the cookeryDao calls in the strategies return a null pointer as the Dao's don't get injected.

我该如何使用spring来实现上述模式,同时又是线程安全的?我正在尝试的甚至有可能吗?

How would I implement the above pattern using spring and also be thread safe? Is what I'm trying even possible?

推荐答案

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

I would use simple Dependency Injection.

@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限定词而不是文本名称,以在编译时捕获拼写错误.

You can choose to create JSR-330 qualifiers instead of textual names to catch misspellings during compile time.

另请参阅:

如何有效地利用Spring实施战略模式?/a>

How to efficiently implement a strategy pattern with spring?

@Resource vs @Autowired

这篇关于春豆战略模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆