Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala和Spring-棘手的案例?_Spring_Scala - Fatal编程技术网

Scala和Spring-棘手的案例?

Scala和Spring-棘手的案例?,spring,scala,Spring,Scala,我想使用工厂方法用Spring实例化一个bean。 然而,诀窍是我想在我的工厂方法中访问当前的ApplicationContext 以下是我尝试的代码: object ActorSystemFactory { var applicationContext: ApplicationContext = _ def createActorSystem = { val system = ActorSystem("AkkaScalaSpring") SpringExtensio

我想使用
工厂方法
用Spring实例化一个bean。 然而,诀窍是我想在我的工厂方法中访问当前的
ApplicationContext

以下是我尝试的代码:

object ActorSystemFactory {

  var applicationContext: ApplicationContext = _

  def createActorSystem = {
    val system = ActorSystem("AkkaScalaSpring")
    SpringExtensionImpl(system)(applicationContext) //need applicationContext here
    system
  }
}

class ActorSystemFactory extends ApplicationContextAware {

  //Spring warns that createActorSystem does not exist here!! and it is...right
  //Indeed, it is present in the companion object to make it "static" equivalent.    

  def setApplicationContext(applicationContext: ApplicationContext) {
    ActorSystemFactory.applicationContext = applicationContext
  }

}
我的xml声明:

<bean id="actorSystem" class="com.myPackage.ActorSystemFactory" factory-method="createActorSystem">
</bean>


关于
ActorSystemFactory
类中的注释,如何处理该情况?

Springbean默认为singleton。您可以在类中直接实例化您的actor系统,不需要对象,所以我设法做到了这一点

事实上,现在我不使用静态工厂方法技术,而是使用实例工厂方法技术:

class ActorSystemFactory extends ApplicationContextAware {

  def createActorSystem = {
    val system = ActorSystem("AkkaScalaSpring")
    SpringExtensionImpl(system)
    system
  }

  def setApplicationContext(applicationContext: ApplicationContext) {
    ActorSystemFactory.applicationContext = applicationContext
  }
}

object ActorSystemFactory {

  var applicationContext: ApplicationContext = _

}



<bean id="actorSystemFactory" class="myPackage.ActorSystemFactory"/>

<bean id="actorSystem" factory-bean="actorSystemFactory" factory-method="createActorSystem" />
class ActorSystemFactory扩展了ApplicationContextAware{
def createActorSystem={
val系统=ActorSystem(“阿克斯卡斯卡拉斯普林”)
SpringExtensionImpl(系统)
系统
}
def setApplicationContext(applicationContext:applicationContext){
ActorSystemFactory.applicationContext=applicationContext
}
}
对象ActorSystemFactory{
var applicationContext:applicationContext=_
}
()(无论Spring的版本是什么)


整个工作如预期。

如果我这样做,则
createActorSystem
方法将不会是“静态”的。