Spring Bean Custom Settings

I am using the Spring Akka example hosted on an activator to create Spring managed bean members. This is the code I'm currently using, including a demo class:

@Component class Test extends UntypedActor { @Autowired protected ObjectMapper objectMapper; protected final Account account; protected final Order order; public Test(Account account, Order order) { this.account = account; this.order = order; } @Override public void onReceive(Object message) throws Exception { if (message instanceof SomeCommand) { // Do something using the order and the account; } else if (message instanceof FooCommand) { // More stuff } } } @Component public class SpringExtension extends AbstractExtensionId<SpringExtensionImpl> implements ExtensionIdProvider { @Autowired private ApplicationContext applicationContext; @Override public SpringExtensionImpl createExtension(ExtendedActorSystem system) { return applicationContext.getBean(SpringExtensionImpl.class); } @Override public ExtensionId<? extends Extension> lookup() { return applicationContext.getBean(SpringExtension.class); } } @Component public class SpringExtensionImpl implements Extension { @Autowired private ApplicationContext applicationContext; public Props props(String actorBeanName) { return Props.create(SpringActorProducer.class, applicationContext, actorBeanName); } } public class SpringActorProducer implements IndirectActorProducer { private final ApplicationContext applicationContext; private final String actorBeanName; public SpringActorProducer(ApplicationContext applicationContext, String actorBeanName) { this.applicationContext = applicationContext; this.actorBeanName = actorBeanName; } @Override public Actor produce() { return (Actor) applicationContext.getBean(actorBeanName); } @Override public Class<? extends Actor> actorClass() { return (Class<? extends Actor>) applicationContext.getType(actorBeanName); } } 

Now my question is: how to instantiate an actor using custom constructor arguments. I thought about using factory or setter methods, but I donโ€™t think this is an option because the base class of Actor is not available, I think. Any input on this is greatly appreciated. If something became clear, send a comment.

PS. If you think there is an error in my code, or there is a better way around this, please tell me! I have little experience with Spring and Akka, so any advice is appreciated.

+6
source share
1 answer

You can pass additional arguments like varargs ( Object... ) to SpringExtensionImpl and SpringActorProducer . So your code will look like this:

 @Component public class SpringExtensionImpl implements Extension { @Autowired private ApplicationContext applicationContext; public Props props(String actorBeanName, Object... args) { return (args != null && args.length > 0) ? Props.create(SpringActorProducer.class, applicationContext, actorBeanName, args) : Props.create(SpringActorProducer.class, applicationContext, actorBeanName); } } public class SpringActorProducer implements IndirectActorProducer { private final ApplicationContext applicationContext; private final String actorBeanName; private final Object[] args; public SpringActorProducer(ApplicationContext applicationContext, String actorBeanName) { this.applicationContext = applicationContext; this.actorBeanName = actorBeanName; this.args = null; } public SpringActorProducer(ApplicationContext applicationContext, String actorBeanName, Object... args) { this.applicationContext = applicationContext; this.actorBeanName = actorBeanName; this.args = args; } @Override public Actor produce() { return args == null ? (Actor) applicationContext.getBean(actorBeanName): (Actor) applicationContext.getBean(actorBeanName, args); } @Override public Class<? extends Actor> actorClass() { return (Class<? extends Actor>) applicationContext.getType(actorBeanName); } } 

Then you can create your test actor as follows:

SpringExtensionImpl springExtensionImpl; actorSystem.actorOf(springExtensionImpl.create(Test.class, account, order));

+3
source

Source: https://habr.com/ru/post/978283/


All Articles