问题
I would like to iterate over all the available dataSources for the current environment, trying to see if any of them contain a particular domain instance. Each environment has different dataSources.
The best I've hacked up so far is grailsApplication.config.findAll { it.key.contains('dataSource_') }
. Is there a better or more legitimate way to do this?
回答1:
You can also query against the bean names in the application context.
Something like
ctx.beanDefinitionNames.findAll{ it.contains( 'dataSource' ) }
At least that is what the DatasourcesUtils in the Datasources plugin does -
http://plugins.grails.org/grails-datasources/trunk/src/groovy/com/burtbeckwith/grails/plugin/datasources/DatasourcesUtils.groovy
来源:https://stackoverflow.com/questions/9708067/can-i-list-all-of-the-datasources-available