Armen Arzumanyan
Armen Arzumanyan

Reputation: 2053

Could not inject Spring service into akka service

I have Spring service, which is actually actor, it is received info, but I cant pass it to another Spring service, because injection fails.

@Service("mailContainer")
@Scope("prototype")
@Component
public class MailContainer extends UntypedActor {

    private final LoggingAdapter LOG = Logging.getLogger(getContext().system(), this);

    private Mail value;
    private List<Mail> mailList = new ArrayList<Mail>();
    private Integer size;

    @Autowired
    @Qualifier("springService")
    private SpringService springService;

    //@Autowired
    public void setSpringService(SpringService springService) {
        this.springService = springService;
    }

    public MailContainer(Mail value) {
        this.value = value;
    }

    @Override
    public void onReceive(Object message) throws Exception {

        //    LOG.debug("+ MailContainer message: {} ", message);
        if (message instanceof Mail) {
            value = (Mail) message;
            System.out.println("MailContainer get message with id   " + value.getId());
            System.out.println("With time   " + value.getDateSend());
            //getSender().tell(value, getSelf()); //heta uxarkum
            //this.saveIt(value);
            springService.add(value);
        }

    }

and second service

@Service("springService")
//@Component
@Scope("session")
public class SpringService {

    private List<Mail> mailList = new ArrayList<Mail>();

    public void add(Mail mail) {
        System.out.println("Saving mail from Spring " +mail.getId());
        mailList.add(mail);

    }

    public List<Mail> getMailList() {

        return mailList;
    }

}

Spring config, this is from akka spring example

@Configuration
//@EnableScheduling
//EnableAsync
@ComponentScan(basePackages = {"com"}, excludeFilters = {
    @ComponentScan.Filter(Configuration.class)})
//@ImportResource("classpath:META-INF/spring/spring-data-context.xml")
//@EnableTransactionManagement
//@EnableMBeanExport
//@EnableWebMvc
public class CommonCoreConfig {


 // the application context is needed to initialize the Akka Spring Extension
  @Autowired
  private ApplicationContext applicationContext;

  /**
   * Actor system singleton for this application.
   */
  @Bean
  public ActorSystem actorSystem() {
    ActorSystem system = ActorSystem.create("AkkaJavaSpring");
    // initialize the application context in the Akka Spring Extension
    SpringExtProvider.get(system).initialize(applicationContext);
    return system;
  }
}

So, how I can inject just another Spring service?????????

Upvotes: 1

Views: 1956

Answers (1)

nickebbitt
nickebbitt

Reputation: 1761

Based on our discussions, I think it is due to the way you create the MailContainer actor. You aren't using the SpringExtProvider and instead are using Props.create directly. This means that Spring doesn't get the opportunity to perform dependency injection on your new actor.

Try changing this code:

@Override
public void preStart() throws Exception {
    System.out.println("Mail collector preStart: {} ");
    getContext().actorOf(Props.create(MailContainer.class, result), "one");
}

to use the the SpringExtProvider like this:

@Override
public void preStart() throws Exception {
    System.out.println("Mail collector preStart: {} ");
    getContext().actorOf(SpringExtProvider.get(getContext().system()).props("mailContainer"), "one");
}

This way you are asking the Spring extension to create the new actor and inject any required dependecnies.

Upvotes: 3

Related Questions