1

我正在将Play 2.3.x应用程序迁移到Play 2.5.x,并且在使用依赖注入时遇到了一些问题。如何正确使用DI来注入Play控制器的构造函数?

在2.3中,我有一个特性HasRemoteActor,控制器会根据配置混合引用一些远程参与者。由于这需要应用程序的配置对象,现在要求它变成一个类,因此可以注入配置。这里是我的尝试:

/* 
    Dummy controller that has environment and configuration manually injected. 
*/ 
class ConfigurationController(env: play.api.Environment, 
           conf: play.api.Configuration) extends Controller { 

} 

/* 
    Dummy controller that has environment and configuration manually injected, but 
    sets up a remote client. 
*/ 
class RemoteActorController(env: play.api.Environment, conf: play.api.Configuration) 
    extends ConfigurationController(env, conf) { 

    protected val remoteActorName = "foo" 
    private val remoteActorConf = conf.underlying.getConfig(remoteActorName) 
    private val system = ActorSystem("HttpServerSystem", ConfigFactory.load()) 

    private val tcpInfo = remoteActorConf.getConfig("akka.remote.netty.tcp") 
    private val hostname = tcpInfo.getString("hostname") 
    private val port = tcpInfo.getString("port") 

    val path = s"akka.tcp://[email protected]$hostname:$port/system/receptionist" 

    private val initialContacts = Set(ActorPath.fromString(path)) 


    protected val client = system.actorOf(
    ClusterClient.props(ClusterClientSettings(system).withInitialContacts(
     initialContacts)), 
    "ClusterClient" 
) 
} 

/* 
    Actual controller whose actions correspond to endpoints in `conf/routes`. 
*/ 
@Singleton 
class BarController @Inject()(env: play.api.Environment, 
           conf: play.api.Configuration) extends 
    RemoteActorController(env, conf) { 

    // ... 

} 

然而,当我开始我的申请,我觉得演员系统始终未能找到它的端口(即使没有什么是侦听该端口),不考虑的端口号。

play.api.UnexpectedException: Unexpected exception[ProvisionException: Unable to provision, see the following errors: 

1) Error injecting constructor, org.jboss.netty.channel.ChannelException: Failed to bind to: /127.0.0.1:8888 

似乎是与注射的时机的问题,但我很新的给我在遇到麻烦调试它DI。

我试着在我的build.sbt中加入routesGenerator := InjectedRoutesGenerator,并在@前加上了我的注入路由的相关控制器,但仍然发现相同的运行时异常。

有没有人有建议?

+0

“无法绑定”通常意味着您已经拥有使用此端口的应用程序,请尝试将您的配置更改为使用其他端口。 – vdebergue

+0

@vdebergue这些端口没有被TCP或UDP使用。 – erip

回答

1

我不会为此使用继承。相反,我会去这样的事情(我要你用Guice的假设):

@Singleton 
class RemoteActorAdapter @Inject() (env: Environment, conf: Configuration) { 

    // all other initialization code 
    val client: ActorRef = ??? 

} 

在想要使用这些东西的控制:

class MyController @Inject() (remoteAdapterProvider: Provider[RemoteActorAdapter]) extends Controller { 
    def index = Action { 
    remoteAdapterProvider.get.client ! Hello 
    } 
} 

这样的伎俩是通过使用提供者,您将绑定的初始化等推迟到需要的时候。

+0

尽管我比我尝试的更喜欢这种方法,但我仍然发现我遇到绑定错误。 – erip

+0

我认为它没有将'RemoteActorAdapter'绑定为一个singleton ...你知道如何在'Module.scala'中正确地绑定这个吗? – erip

+0

所以使用'Provider'的目的是推迟初始化。初始化是否是第一次工作,但后来失败了,因为它没有重新使用实例?还是在第一次尝试时也失败?在前一种情况下,您可以尝试如下操作:在(新的SingletonScope())'中绑定(classOf [RemoteActorAdapter])。到(classOf [RemoteActorAdapter])。在后一种情况下,我怀疑这与依赖注入有什么关系。 – rethab

相关问题