Guice Integration

Guice Integration

Module stability: STABLE

All Typed Actors support dependency injection using Guice annotations (such as ‘@Inject’ etc.). The ‘TypedActorManager’ class understands Guice and will do the wiring for you.

External Guice modules

You can also plug in external Guice modules and have not-actors wired up as part of the configuration. Here is an example:

  1. import static akka.config.Supervision.*;
  2. import static akka.config.SupervisorConfig.*;
  3.  
  4. TypedActorConfigurator manager = new TypedActorConfigurator();
  5.  
  6. manager.configure(
  7. new AllForOneStrategy(new Class[]{Exception.class}, 3, 1000),
  8. new SuperviseTypedActor[] {
  9. new SuperviseTypedActor(
  10. Foo.class,
  11. FooImpl.class,
  12. temporary(),
  13. 1000),
  14. new SuperviseTypedActor(
  15. Bar.class,
  16. BarImpl.class,
  17. permanent(),
  18. 1000)
  19. })
  20. .addExternalGuiceModule(new AbstractModule() {
  21. protected void configure() {
  22. bind(Ext.class).to(ExtImpl.class).in(Scopes.SINGLETON);
  23. }})
  24. .configure()
  25. .inject()
  26. .supervise();

Retrieve the external Guice dependency

The external dependency can be retrieved like this:

  1. Ext ext = manager.getExternalDependency(Ext.class);