将配置和服务注入Actor

时间:2016-03-20 21:24:22

标签: playframework playframework-2.0 actor

在Play的网站上documentation之后,我的演员配置如下;

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:没有在类的actors.SuggestionActor上找到匹配的构造函数[]

想法?

1 个答案:

答案 0 :(得分:0)

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