将配置和服务注入 Actor

Injecting Configuration and Service into an Actor

按照Play网站上的documentation,我的Actor配置如下;

public class SuggestionActor extends UntypedActor {


    public static Props props = Props.create(SuggestionActor.class);

    private DAOService service;
    private Configuration config;

    @Inject
    public SuggestionActor(DAOService service, Configuration config) {
        this.service = service;
        this.config = config;
    }


    @Override
    public void onReceive(Object msg) throws Exception {
        if(msg instanceof SayHello) {
            // check if msg comes
            Logger.info(((SayHello) msg).name + config.getString("dao.mode"));
        }
    }
}

-

public class ActorProtocols {

    public static class SayHello{
        public final String name;


        public SayHello(String name) {
            this.name = name;
        }
    }
}

MyModule(在 application.conf 中启用)

public class MyModule extends AbstractModule implements AkkaGuiceSupport {

    @Override
    protected void configure() {
        bindActor(SuggestionActor.class, "suggestion-actor");
    }
}

我的控制器

@Singleton
public class SuggestionController extends Controller {

    private static Logger.ALogger LOGGER = Logger.of(SuggestionController.class);

    @Inject @Named("suggestion-actor")
    private ActorRef suggestionActor;

    public Result suggest(String message) {
         ask(suggestionActor, new SayHello(message), 10000);
    }
}

如果我尝试在 SuggestionActor 中通过构造函数注入来注入我的 DAO 对象和配置,Play 会抛出一个 原因:java.lang.IllegalArgumentException:在 class actors.SuggestionActor 上找不到参数 []

的匹配构造函数

想法?

我尝试使用 Guice 将组件注入 SuggestionActor,同时使用 Props(在我的控制器中)。一旦我删除了道具,一切都开始正常工作。注意,如果要使用 Props,请遵循此 SO Post.