【发布时间】:2013-07-28 13:03:48
【问题描述】:
所以基本上我是在尝试让我的项目在 AppFog 上启动并运行。数据源信息存储在环境变量中,本质上是 JSON。我的目标是获取这些数据并从中设置我的数据源配置。
这是我尝试过的:
设置数据源配置的代码,这是 POGO 中的一种方法。实例化POGO并在DataSource.groovy开头调用方法:
import appfog.ParseDataSource
new ParseDataSource().setConfig()
dataSource {
...
}
class ParseDataSource {
void setConfig() {
String env = java.lang.System.getenv("VCAP_SERVICES")
if (env) {
def config = JSON.parse(env)
config = config["mysql-5.1"][0].credentials
grailsApplication.config.environments.production.dataSource.username = config.username
grailsApplication.config.environments.production.dataSource.password = config.password
grailsApplication.config.environments.production.dataSource.url = "jdbc:mysql://" + config.host + ":" + config.port + "/" + config.name
}
}
}
问题在于 grailsApplication 始终为空。我尝试在 resources.groovy 中注册一个 spring bean:
beans = {
parseDataSource(appfog.ParseDataSource) {
grailsApplication = ref('grailsApplication')
}
}
class ParseDataSource {
def grailsAPplication
...
}
我也尝试过通过 Holders 获得它:
GrailsApplication grailsApplication = Holders.grailsApplication
无论哪种方式,它都是空的,所以我没有做正确的事情。有什么想法吗?
【问题讨论】:
-
使用
new手动实例化的类不参与 bean 自动装配。
标签: grails datasource appfog