我有Spring服务,它实际上是actor,它是收到信息,但我不能将它传递给另一个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服务?????????
答案 0 :(得分:3)
根据我们的讨论,我认为这是由于您创建MailContainer actor的方式。您没有使用SpringExtProvider,而是直接使用Props.create。这意味着Spring没有机会对新演员执行依赖注入。
尝试更改此代码:
@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扩展创建新的actor并注入任何必需的dependecnies。