0

我已经在 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()
    }
}
4

1 回答 1

0

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

于 2019-01-09T16:58:36.940 回答