【问题标题】:How does Guice Assisted-Injection select which object to bindGuice Assisted-Injection 如何选择要绑定的对象
【发布时间】:2018-04-13 19:06:28
【问题描述】:

我已经在 Scala 中实现了 Guice 辅助注入。在下面我的简化示例中,它注入名为“config”的 Config 对象,而不是名为“dataSourceConfig”的 Config 对象。

由于没有明确引用通过辅助注入的参数,它如何知道要注入哪个对象?

显然,我可以解决这个问题,但我更愿意了解这里发生了什么。

提前致谢。

class ServiceModule extends AbstractModule {
    override def configure(): Unit = {      
        val config: Config = ConfigUtil.getConfig // This reads a .conf file and parses into a Typesafe Config object.
        val dataSourceConfig: Config = config.getConfig("dataSource") // ignore magic word

        // Two Config objects are defined here. How does it choose?
        install(new FactoryModuleBuilder().implement(classOf[DataSourceService], classOf[DataSourceServiceImpl])
            .build(classOf[DataSourceServiceFactory]))
    }
}

trait DataSourceServiceFactory {
    def create(dataSourceConfig: Config): DataSourceService
}

@ImplementedBy(classOf[DataSourceServiceImpl])
trait DataSourceService {
    def fetchData(id: Int): DataObject
}

class DataSourceServiceImpl @Inject()(@Assisted dataSourceConfig: Config) extends DataSourceService {
    override def fetchData(id: Int): DataObject = {
        DataObject()
    }
}

【问题讨论】:

    标签: scala guice assisted-inject


    【解决方案1】:

    这篇文章应该被删除。这表明我当时的理解还不够,无法提出一个明智的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-28
      • 1970-01-01
      • 2016-08-06
      • 2012-04-08
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多