简体   繁体   English

Scala和Slick:独立应用程序中的DatabaseConfigProvider

[英]Scala and Slick: DatabaseConfigProvider in standalone application

I have an Play 2.5.3 application which uses Slick for reading an object from DB. 我有一个Play 2.5.3应用程序,该应用程序使用Slick从DB读取对象。 The service classes are built in the following way: 服务类以以下方式构建:

class SomeModelRepo @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) {
  val dbConfig = dbConfigProvider.get[JdbcProfile]
  import dbConfig.driver.api._
  val db = dbConfig.db
  ...

Now I need some standalone Scala scripts to perform some operations in the background. 现在,我需要一些独立的Scala脚本在后台执行一些操作。 I need to connect to the DB within them and I would like to reuse my existing service classes to read objects from DB. 我需要连接到其中的数据库,并且我想重用现有的服务类来从数据库读取对象。

To instantiate a SomeModelRepo class' object I need to pass some DatabaseConfigProvider as a parameter. 要实例化SomeModelRepo类的对象,我需要传递一些DatabaseConfigProvider作为参数。 I tried to run: 我试着跑:

object SomeParser extends App {
    object testDbProvider extends DatabaseConfigProvider {
      def get[P <: BasicProfile]: DatabaseConfig[P] = {
        DatabaseConfigProvider.get("default")(Play.current)
      }
    }
    ...
    val someRepo = new SomeModelRepo(testDbProvider)

however I have an error: "There is no started application" in the line with "(Play.current)". 但是我有一个错误:“(Play.current)”所在行中的“没有启动的应用程序”。 Moreover the method current in object Play is deprecated and should be replaced with DI. 此外,不赞成使用对象Play中的current方法,应将其替换为DI。

Is there any way to initialize my SomeModelRepo class' object within the standalone object SomeParser? 有什么方法可以在独立对象SomeParser中初始化SomeModelRepo类的对象?

Best regards 最好的祝福

When you start your Play application, the PlaySlick module handles the Slick configurations for you. 启动Play应用程序时,PlaySlick模块将为您处理Slick配置。 With it you have two choices : 有了它,您有两种选择

  1. inject DatabaseConfigProvider and get the driver from there, or 注入DatabaseConfigProvider并从那里获取驱动程序,或者
  2. do a global lookup via DatabaseConfigProvider.get[JdbcProfile](Play.current) , which is not preferred. 通过DatabaseConfigProvider.get[JdbcProfile](Play.current)进行全局查找,这不是首选方法。

Either way, you must have your Play app running! 无论哪种方式,您都必须运行Play应用! Since this is not the case with your standalone scripts you get the error: "There is no started application". 由于独立脚本不是这种情况,因此会出现错误:“没有启动的应用程序”。

So, you will have to use Slick's default approach, by instantiating db directly from config: 因此,您将必须通过直接从config实例化db来使用Slick的默认方法:

val db = Database.forConfig("default")

You have lot's of examples at Lightbend's templates . Lightbend的模板上有很多示例。

EDIT: Sorry, I didn't read the whole question. 编辑:对不起,我没有阅读整个问题。 Do you really need to have it as another application? 您是否真的需要将其作为另一个应用程序? You can run your background operations when your app starts, like here . 您可以在应用启动时运行后台操作,例如此处 In this example, InitialData class is instantiated as eager singleton , so it's insert() method is run immediately when app starts. 在此示例中, InitialData类被实例化为eager singleton ,因此它的insert()方法在应用程序启动时立即运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM