@@ -6,6 +6,7 @@ JVM User Language Support for [Spawn](https://github.com/eigr/spawn).
661 . [ Overview] ( #overview )
772 . [ Getting Started] ( #getting-started )
883 . [ Advanced Use Cases] ( #advanced-use-cases )
9+ - [ Dependency Injection] ( #dependency-injection )
910 - [ Types of Actors] ( #types-of-actors )
1011 - [ Stateless Actors] ( #stateless-actors )
1112 - [ Considerations about Spawn actors] ( #considerations-about-spawn-actors )
@@ -14,16 +15,16 @@ JVM User Language Support for [Spawn](https://github.com/eigr/spawn).
1415 - [ Forward] ( #forward )
1516 - [ Pipe] ( #pipe )
1617 - [ State Management] ( #state-management )
17- 4 . [ Using Actors] ( #using-actors )
18+ 5 . [ Using Actors] ( #using-actors )
1819 - [ Call Named Actors] ( #call-named-actors )
1920 - [ Call Unnamed Actors] ( #call-unnamed-actors )
2021 - [ Async] ( #async )
2122 - [ Timeouts] ( #timeouts )
22- 5 . [ Deploy] ( #deploy )
23+ 6 . [ Deploy] ( #deploy )
2324 - [ Defining an ActorSystem] ( #defining-an-actorsystem )
2425 - [ Defining an ActorHost] ( #defining-an-actorhost )
2526 - [ Activators] ( #activators )
26- 6 . [ Actor Model] ( #actor-model )
27+ 7 . [ Actor Model] ( #actor-model )
2728 - [ Virtual Actors] ( #virtual-actors )
2829
2930
@@ -93,7 +94,7 @@ The second thing we have to do is add the spawn dependency to the project.
9394<dependency >
9495 <groupId >com.github.eigr</groupId >
9596 <artifactId >spawn-java-std-sdk</artifactId >
96- <version >v0.9.1 </version >
97+ <version >v1.0.0 </version >
9798</dependency >
9899```
99100We're also going to configure a few things for our application build to work, including compiling the protobuf files.
@@ -127,7 +128,7 @@ See below a full example of the pom.xml file:
127128 <dependency >
128129 <groupId >com.github.eigr</groupId >
129130 <artifactId >spawn-java-std-sdk</artifactId >
130- <version >v0.9.1 </version >
131+ <version >v1.0.0 </version >
131132 </dependency >
132133 <dependency >
133134 <groupId >ch.qos.logback</groupId >
@@ -518,6 +519,102 @@ Spawn Actors abstract a huge amount of developer infrastructure and can be used
518519In the sections below we will demonstrate some features available in Spawn that contribute to the development of
519520complex applications in a simplified way.
520521
522+ ### Dependency Injection
523+
524+ Sometimes we need to pass many arguments as dependencies to the Actor class.
525+ In this case, it is more convenient to use your own dependency injection mechanism.
526+ However, the Spawn SDK already comes with an auxiliary class to make this easier for the developer.
527+ Let's look at an example:
528+
529+ 1 . First let's take a look at some example dependency classes:
530+
531+ ``` java
532+ // We will have an interface that represents any type of service.
533+ public interface MessageService {
534+ String getDefaultMessage ();
535+ }
536+
537+ // and concrete implementation here
538+ public class MessageServiceImpl implements MessageService {
539+ @Override
540+ public String getDefaultMessage () {
541+ return " Hello Spawn in English" ;
542+ }
543+ }
544+ ```
545+
546+ 2 . Second, let's define an actor so that it receives an instance of the DependencyInjector class through the class constructor:
547+
548+ ``` java
549+ package io.eigr.spawn.test.actors ;
550+
551+ import io.eigr.spawn.api.actors.ActorContext ;
552+ import io.eigr.spawn.api.actors.Value ;
553+ import io.eigr.spawn.api.actors.annotations.Action ;
554+ import io.eigr.spawn.api.actors.annotations.stateful.StatefulNamedActor ;
555+ import io.eigr.spawn.api.extensions.DependencyInjector ;
556+ import io.eigr.spawn.java.test.domain.Actor ;
557+
558+ @StatefulNamedActor (name = " test_actor_constructor" , stateType = Actor . State . class)
559+ public final class ActorWithDependencies {
560+
561+ private final MessageService messageService;
562+
563+ public ActorWithConstructor (DependencyInjector injector ) {
564+ // Note how to use dependency injection here to get a concrete class of MessageService.
565+ this . defaultMessage = injector. getInstance(MessageService . class);
566+ }
567+
568+ @Action (inputType = Actor . Request . class)
569+ public Value setLanguage (Actor .Request msg , ActorContext<Actor . State > context ) {
570+ if (context. getState(). isPresent()) {
571+ }
572+
573+ return Value . at()
574+ .response(Actor . Reply . newBuilder()
575+ .setResponse(messageService. getDefaultMessage())
576+ .build())
577+ .state(updateState(" java" ))
578+ .reply();
579+ }
580+
581+ private Actor .State updateState (String language ) {
582+ return Actor . State . newBuilder()
583+ .addLanguages(language)
584+ .build();
585+ }
586+ }
587+
588+ ```
589+
590+ 3 . Then you can pass your dependent classes this way to your Actor:
591+ ``` java
592+ package io.eigr.spawn.java.demo ;
593+
594+ import io.eigr.spawn.api.Spawn ;
595+ import io.eigr.spawn.api.extensions.DependencyInjector ;
596+ import io.eigr.spawn.api.extensions.SimpleDependencyInjector ;
597+
598+ public class App {
599+ public static void main (String [] args ) {
600+ DependencyInjector injector = SimpleDependencyInjector . createInjector();
601+ injector. bind(MessageService . class, new MessageServiceImpl ());
602+
603+ Spawn spawnSystem = new Spawn .SpawnSystem ()
604+ .create(" spawn-system" )
605+ .withActor(Joe . class, actorConstructorArgs, injector - > new Joe ((DependencyInjector ) injector))
606+ .build();
607+
608+ spawnSystem. start();
609+ }
610+ }
611+ ```
612+
613+ It is important to note that this helper mechanism does not currently implement any type of complex dependency graph.
614+ Therefore, it will not build objects based on complex dependencies nor take care of the object lifecycle for you.
615+ In other words, all instances added through the bind method of the SimpleDependencyInjector class will be singletons.
616+ This mechanism works much more like a bucket of objects that will be forwarded via your actor's constructor.
617+
521618### Types of Actors
522619
523620First we need to understand how the various types of actors available in Spawn behave. Spawn defines the following types of Actors:
0 commit comments