开发者

How to get grails datasource createdb property in bootstrap

I'd like to make my Bootstrap dependant on the createdb property in the grails DataSource.groovy file. When the setting is 'create', new Master Data should be generated, if the setting is 'update', none.

I've found GrailsDataSource in the Grails API which also has a m开发者_如何学Cethod getCreateDb, but I don't know how to access it from the Bootstrap for the respective Bootstrap environment.


The up-to-date way to obtain dbCreate property is injecting 'grailsApplication' to BootStrap.groovy:

def grailsApplication

if(grailsApplication.config.dataSource.dbCreate == 'create'){
    ...
}

ApplicationHolder is now deprecated and I couldn't get it to work.

Hope it helps someone.


The easiest way is probably to just check the value of dbCreate from the config, like so:

import org.codehaus.groovy.grails.commons.ApplicationHolder

if (ApplicationHolder.application.config.dataSource.dbCreate == "create") {
    ...do create stuff...
} else if (ApplicationHolder.application.config.dataSource.dbCreate == "update") {
    ...do update stuff...
}


Answer from ataylor is deprecated. Please refer to How to access Grails configuration in Grails 2.0?.

Also it should be noted in canotto90 answer that injection def grailsApplication should appear outside of init closure of BootStrap. In other way u get NPE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜