所以本质上我正在尝试让我的项目在 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
无论哪种方式,它都是空的,所以我没有做正确的事情。有任何想法吗?