【问题标题】:Inject controller in actor在actor中注入控制器
【发布时间】:2015-12-01 10:29:35
【问题描述】:

是否可以在 UntypedActor 中注入控制器类? 连接到控制器类的最佳方式是什么?

package actors;

import akka.actor.UntypedActor;
import dispatchers.PushNotificationDispatcher;
import play.Logger;

import javax.inject.Inject;
import java.util.List;

public class PushNotificationActor extends UntypedActor {

    @Inject
    PushNotificationDispatcher dispatcher;

    @Override
    public void onReceive(Object message) throws Exception {
        Logger.debug("PushNotificationActor started");
        dispatcher.createAndSendReminderPushNotification();
        Logger.debug("PushNotificationActor finished");
    }

}

【问题讨论】:

    标签: playframework playframework-2.0 playframework-2.4


    【解决方案1】:

    我认为不可能像您写的那样做到这一点...Actor 是封装的类。最好的选择是检查此代码是否有效,但 IMO 它不会...

    您可以尝试通过以下参数传递此注入:

    Props props2 = Props.create(MyActor.class, your params);
    

    【讨论】:

      【解决方案2】:

      是的,可以将实例注入到您的 Actor 中。

      使用 Play 2.4 它可以工作 out of the box,就像在普通课程中一样注入。

      我总是注入到构造函数中,例如这里是 JPAApi:

      public class MyActor extends UntypedActor {
      
        private final JPAApi jpa;
      
        @Inject
        public MyActor(JPAApi jpa) {
          this.jpa = jpa;
        }
      
        @Override
        public void onReceive(Object msg) throws Exception {
          ...
        }
      }
      

      【讨论】:

      • 我尝试实现代码,但在我的情况下,注入的控制器始终为空。你有一些示例代码吗?
      • 我添加了一个例子。希望对您有所帮助。
      • 这很棘手,我也试过你的代码,但是我有一个 NullPointerException 我在github上添加了我的代码github.com/aBuder/play-actor-sample
      猜你喜欢
      • 1970-01-01
      • 2021-01-01
      • 2013-01-25
      • 2014-09-09
      • 2014-11-06
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多