本文介绍了如何通过CDI实现命令模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CDI的新手,有点困惑。我有以下问题。我们有课程动作。我们有包装类,它将所有Action对象保存在hashmap中。这样的事情。

I am newbie in CDI and a little bit confused. I have the following problem. We have class Action. And we have wrapper class that keeps all Action objects in hashmap. Something like this.

class TestAction implements Action{
  @EJB
  private MyBean bean; 
  public void doSomething(){
    //here we do something with injected EJB
  }
}

class Foo {
   private HashMap<String, Action> hm;
   public void execute (String action){
   this.hm.get(action).doSomething();    
 }    
}

当我不使用CDI时 - 一切正常。但我需要使用它。因此,据我所知,我必须通过cdi容器创建所有操作,否则CDI容器无法将托管bean注入其中。所以我的问题是什么是通过CDI实现命令模式的最佳方法?

When I don't use CDI - everything is ok. But I need to use it. So, as I understand I must create all my actions via cdi container otherwise CDI container can't inject managed beans into them. So my question what is the best way to implement command pattern via CDI?

编辑:
我读了Dhanji R的依赖注入.Prasanna,Weld-reference(WR),JavaEE7教程(CDI部分) - 不建议阅读最后一篇。在思考了一点之后,我明白我需要注入HashMap。此外,我明白我必须使用生产者方法。好。我说。终于我明白了。所以我编写了以下生成器方法:

EDITED:I read Dependency Injection by Dhanji R. Prasanna, Weld-reference(WR), JavaEE7 tutorial (CDI part) - don't recommend to read the last. After thinking a little bit I understood that I need to inject HashMap. Besides, I understood that I have to use producer methods. Ok. I said. Finally I got it. So I wrote the following producer method:

@ApplicationScoped
public class ActionMapFactory {
    @Produces @Preffered
    public HashMap<String, Action> getHashMap(){
    HashMap<String, Action> hm=new HashMap<>();
     if (...){
     hm.put("save",new SaveAction());
     }
     return hm;
    }
}

来自WR:

我已经阅读过WR的解决方案,但如果我有很多动作和很多Foo的子类,我该怎么办?

I have read the solution from WR, but what should I do if I have dozens of actions and a lot child classes of Foo?

推荐答案

您可以通过将所有操作注入<$ c来避免 new $ c> ActionMapFactory 并在生产者方法中填充 HashMap

You could avoid new by injecting all actions into ActionMapFactory and populate the HashMap at the producer method:

@ApplicationScoped
public class ActionMapFactory {

    @Inject
    private SaveAction saveAction;

    @Inject
    private DeleteAction deleteAction;

    // And so on

    @Produces @Preffered
    public HashMap<String, Action> getHashMap() {
        Map<String, Action> hm = new HashMap<>();
        hm.put("save", saveAction);
        hm.put("delete", deleteAction);
        return hm;
    }
}

如果你不想保留那些 Action 实例作为属性,进行构造函数注入:

If you don't want to keep those Action instances as attributes, do a constructor injection:

private Map<String, Action> actionMap;

// This is part of the CDI bean contract
protected ActionMapFactory() {}

@Inject
public ActionMapFactory(SaveAction saveAction, DeleteAction deleteAction) {
    actionMap = new HashMap<>();
    actionMap.put("save", saveAction);
    actionMap.put("delete", deleteAction);
}

@Produces @Preffered
public HashMap<String, Action> getHashMap() {
    return actionMap;
}

这篇关于如何通过CDI实现命令模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 14:27