1

我有 Spring 服务,它实际上是演员,它接收到信息,但我不能将它传递给另一个 Spring 服务,因为注入失败。

@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);
        }

    }

和第二次服务

@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 配置,这是来自 akka spring 示例

@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;
  }
}

那么,我如何才能注入另一个 Spring 服务?????????

4

1 回答 1

3

根据我们的讨论,我认为这是由于您创建 MailContainer 演员的方式。您没有使用 SpringExtProvider 而是直接使用 Props.create 。这意味着 Spring 没有机会对新的 Actor 执行依赖注入。

尝试更改此代码:

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

像这样使用 SpringExtProvider:

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

通过这种方式,您要求 Spring 扩展创建新参与者并注入任何所需的依赖项。

于 2014-08-13T09:45:44.693 回答