所以基本上我試圖讓我的項目在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
...
}
我還試圖通過持有人得到它:
GrailsApplication grailsApplication = Holders.grailsApplication
無論哪種方式,爲空,所以我沒有做正確的事情。有任何想法嗎?
使用'new'手動實例化的類不參與bean自動裝配。 – codelark