I\'m getting null pointer exception on the field injection of a server which is started as an akka actor.
Schedular part:
private Ac
There can be other ways, eg, you can put a static injector handle in Boot or Some-Injector-Holder-class, and inject when you create the actor by call the inherited method: preStart ()
public class Retreiver extends UntypedActor {
private Logger.ALogger log = Logger.of(Retreiver .class);
@Override
public void preStart () {
super.preStart ();
Boot.injector.injectMembers (this);
//Some-Injector-holder.injectMembers (this);
}
@Inject
private myDataService dataService;
@Override
public void onReceive(Object arg0) throws Exception {
if (0 != dataService.getDataCount()) {
....
....
....
}
}
and also, you can also inject the injector in actor provider to initialize the actor by the injector in a scope of UntypedActorFactory:
public class InjectingActorProvider implements Provider {
@Inject
private Injector injector;
@SuppressWarnings("serial")
@Override
public final ActorRef get() {
Props props = new Props(new UntypedActorFactory() {
@Override
public Actor create() {
return injector.getInstance(actorClass);
}
});
props = props.withRouter(...);
props = props.withDispatcher(...);
actor = system.actorOf(props, actorName);
return actor;
}
}