绑定bean与dataSource

时间:2015-12-07 12:06:47

标签: spring grails datasource

在我的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属性,为什么它不在生产中?如何正确使用?

1 个答案:

答案 0 :(得分:1)

您可以这样使用:

import org.grails.datastore.mapping.core.Datastore

grailsApplication.mainContext.getBeansOfType(Datastore).values().each { d ->
    println d
}

请参阅print语句并找出数据源的bean。