1

在 grails 项目中,会有一个名为DataSource.groovy. 如下:

dataSource {
  pooled = true
  driverClassName = "org.h2.Driver"
  username = "sa"
  password = ""
}

mongodb {
  host = 127.0.0.1// adjust this according to your settings
  port = 27017
  databaseName = 'test'
  username = 'user'  // database user and password, if server requires authentication
  password = 's3cret' 
}

我的问题是如何mongodb.host在运行时动态设置例如。

4

1 回答 1

1

environments如果您有不同的MongoDB主机,您可以使用DataSource.groovy.

在上面的示例中,假设您localhost 127.0.0.1用于开发和mongo-prodserver生产

environments {
   development {
       grails {
           mongo {
               host = "127.0.0.1"
               port = 27017
               username = "user"
               password= "s3cret"
               databaseName = "test"
           }
       }
   }
   production {
       grails {
           mongo {
               host = "mongo-prodserver"
               port = 27017
               username = "user"
               password= "s3cret"
               databaseName = "prod"
           }
       }
   }
   ...
}

这是Grails Doc on DataSources and Environments的链接。

于 2012-09-07T19:42:25.487 回答