在我的ExampleService
中,我尝试使用dataSource_other
获取bean(我必须使用几个db):
class ExampleService {
def grailsApplication
def connectAndCheck(){
def sourceDatabase = grailsApplication.mainContext.getBean('dataSource_other')
}
}
在我的DataSource.groovy中:
environments {
production {
dataSource_information_schema {
ipDbServer = "1.2.3.4"
db = "information_schema"
username = "user"
password = 'pass'
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
url = "jdbc:mysql://${ipDbServer}/${db}?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true"
readOnly = true
}
dataSource_other {
ipDbServer = "1.2.3.5"
db = "other"
username = "user2"
password = 'pass2'
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
url = "jdbc:mysql://${ipDbServer}/${db}?useUnicode=yes&characterEncoding=UTF-8&autoReconnect=true"
}
不幸的是,我有一个错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 bean命名为' dataSource_other'已定义
它在开发环境中工作正常,我从Config
文件中读取dataSource属性,为什么它不在生产中?如何正确使用?
答案 0 :(得分:1)
您可以这样使用:
import org.grails.datastore.mapping.core.Datastore
grailsApplication.mainContext.getBeansOfType(Datastore).values().each { d ->
println d
}
请参阅print语句并找出数据源的bean。