You can use @Provides
When you need code to create an object, use an @Provides method. The method must be defined within a module, and it must have an @Provides annotation. The method's return type is the bound type. Whenever the injector needs an instance of that type, it will invoke the method.
If the @Provides method has a binding annotation like @PayPal or @Named("Checkout"), Guice binds the annotated type. Dependencies can be passed in as parameters to the method. The injector will exercise the bindings for each of these before invoking the method.
@Provides
CreditCardProcessor providePayPalCreditCardProcessor(@Named("PayPal API key") String apiKey) {
PayPalCreditCardProcessor processor = new PayPalCreditCardProcessor();
processor.setApiKey(apiKey);
return processor;
}
However, you should take into account that
Whenever the injector needs an instance of that type, it will invoke the method
So, if you have two components that depend on a Provided component, in this case the CreditCardProcessor
, you'll end up with 2 instances of this component because the Guice injector invokes the method each time it needs to inject an instance of this type.
Scopes provide a solution to this, you have to add the @Singleton
annotation.
@Provides
@Singleton
CreditCardProcessor providePayPalCreditCardProcessor(@Named("PayPal API key") String apiKey) {
PayPalCreditCardProcessor processor = new PayPalCreditCardProcessor();
processor.setApiKey(apiKey);
return processor;
}
In your case it will be
public class SomeMagicalModule extends AbstractModule implements AkkaGuiceSupport {
@Provides
@Singleton
@Named("some-actor")
ActorRef createActor(system: ActorSystem){
system.actorOf(...)
}
}
Credits to codejanovic, see his answer.
Anyway, if you want the reference to the ActorSystem to create Actors, you should use AkkaGuiceSupport (I see that you are already adding it to your module but you don't seem to use it).