Skip to content
Integration between Akka and Guice
Scala
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
LICENSE
NOTICE
README.md
build.sbt

README.md

Akka Guice

Latest release: Pre Release
License: Apache 2.0

AkkaGuice delegates the creation of the Akka actors to Guice.

AkkaGuice is very simple to use. Import the project and use the InjectedProps class provided by Guice to create the actor instead of Props.

@Inject private InjectedProps props;
...
ActorRef ref = system.actorOf(props.create(MyActor.class));

InjectedProps has the same API of the Props Akka version, availabile for Scala and Java.

val ref = system.actorOf(props[MyActor])

The actor is create by Guice so it benefits of the injection (constructor, methods and properites) and the AOP.

public class MyActor {
  private final MyService myService;
  @Inject
  public class MyActor(MyService myService) {
    this.myService = myService;
  }
  ...
}

The actor can be built with not-injected parameters.

ActorRef ref = system.actorOf(props.create(MyActor.class, "Param"));
...
public class MyActor {
  private final MyService myService;
  @Inject
  public class MyActor(MyService myService, String param) {
    this.myService = myService;
  }
  ...
}

They must be different Guice types (or Guice binding key). For example if the actor needs 2 Strings parameters, one of them should be annotated with @Named("...") or another qualifier annotation.

public class MyActor {
  ...
  @Inject
  public class MyActor(MyService myService, String param, @Named("other") param2) {
    this.myService = myService;
  }
  ...
}

AkkaGuice does also a sanity check to avoid the Singleton binding of actor.

You can’t perform that action at this time.