Grails UserCredentialsDataSourceAdapter 配置问题
我是 groovy Grails 框架的新手。我们有一个要求,我们需要在运行时建立数据库连接,因为每个应用程序用户都有唯一的凭据来获取数据库连接。我们发现我们可以利用 Spring 提供的 API UserCredentialsDataSourceAdapter 来实现我们的目标。但是,当我们在添加 UserCredentialsDataSourceAdapter 的配置后尝试运行应用程序时,会收到以下错误
[main] ERROR context.GrailsContextLoader - 执行引导程序时出错:创建名称为“messageSource”的 bean 时出错:bean 初始化失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为“transactionManager”的bean时出错:设置bean属性“sessionFactory”时无法解析对bean“sessionFactory”的引用;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为“sessionFactory”的bean时出错:调用init方法失败;嵌套异常是 java.lang.NullPointerException:无法在 null 对象上获取属性“config” org.springframework.beans.factory.BeanCreationException:创建名称为“messageSource”的bean时出错:bean初始化失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为“transactionManager”的bean时出错:设置bean属性“sessionFactory”时无法解析对bean“sessionFactory”的引用;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为“sessionFactory”的bean时出错:调用init方法失败;嵌套异常是 java.lang.NullPointerException:无法在 null 对象上获取属性“config” 在 org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212) 在 grails.web.container.EmbeddableServer$start.call(来源未知) 在 _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158) 在 _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy) 在 _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280) 在 _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy) 在 _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149) 在 _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy) 在 _GrailsRun_groovy.runInline(_GrailsRun_groovy:116) 在 _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy) 在 _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59) 在 RunApp$_run_closure1.doCall(RunApp:33) 在 gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381) 在 gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415) 在 gant.Gant$_dispatch_closure7.doCall(Gant.groovy) 在 gant.Gant.withBuildListeners(Gant.groovy:427) 在 gant.Gant.this$2$withBuildListeners(Gant.groovy) 在 gant.Gant$this$2$withBuildListeners.callCurrent(来源未知) 在 gant.Gant.dispatch(Gant.groovy:415) 在 gant.Gant.this$2$dispatch(Gant.groovy) 在 gant.Gant.invokeMethod(Gant.groovy) 在 gant.Gant.executeTargets(Gant.groovy:590) 在 gant.Gant.executeTargets(Gant.groovy:589) 引起:org.springframework.beans.factory.BeanCreationException:创建名称为“transactionManager”的bean时出错:设置bean属性“sessionFactory”时无法解析对bean“sessionFactory”的引用;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为“sessionFactory”的bean时出错:调用init方法失败;嵌套异常是 java.lang.NullPointerException:无法在 null 对象上获取属性“config” ... 23 更多 导致:org.springframework.beans.factory.BeanCreationException:创建名称为“sessionFactory”的bean时出错:调用init方法失败;嵌套异常是 java.lang.NullPointerException:无法在 null 对象上获取属性“config” ... 23 更多 由以下原因引起:java.lang.NullPointerException:无法在空对象上获取属性“config” 在 Script1.run(Script1.groovy:1) ... 23 更多
我们在 resources.groovy 中配置了 UserCredentialsDataSourceAdapter,如下
beans = {
targetDataSource(DriverManagerDataSource){
driverClassName= "oracle.jdbc.driver.OracleDriver"
url= "jdbc:oracle:oci:@dbname"
}
dataSource(UserCredentialsDataSourceAdapter){
targetDataSource= ref("targetDataSource")
username = "username"
password = "password"
}
sessionFactory(ConfigurableLocalSessionFactoryBean) {
dataSource = dataSource
hibernateProperties = [ "hibernate.hbm2ddl.auto":"create-drop",
"hibernate.show_sql":true ,
"hibernate.dialect":"org.hibernate.dialect.O racle10gDialect" ]
}
}
I am a newbie to groovy Grails frameworks. We have a requirement wherein we need to establish the database connection at runtime since each application user has unique credentials for getting the db connection. We found that we could make use of Spring provided API UserCredentialsDataSourceAdapter for achieving our goal. But when we try running the application after adding the configuration for UserCredentialsDataSourceAdapter, we get the following error
[main] ERROR context.GrailsContextLoader - Error executing bootstraps: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot get property 'config' on null object
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot get property 'config' on null object
at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
at grails.web.container.EmbeddableServer$start.call(Unknown Source)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
at RunApp$_run_closure1.doCall(RunApp:33)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot get property 'config' on null object
... 23 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.NullPointerException: Cannot get property 'config' on null object
... 23 more
Caused by: java.lang.NullPointerException: Cannot get property 'config' on null object
at Script1.run(Script1.groovy:1)
... 23 more
we have configured the UserCredentialsDataSourceAdapter in the resources.groovy as follows
beans = {
targetDataSource(DriverManagerDataSource){
driverClassName= "oracle.jdbc.driver.OracleDriver"
url= "jdbc:oracle:oci:@dbname"
}
dataSource(UserCredentialsDataSourceAdapter){
targetDataSource= ref("targetDataSource")
username = "username"
password = "password"
}
sessionFactory(ConfigurableLocalSessionFactoryBean) {
dataSource = dataSource
hibernateProperties = [ "hibernate.hbm2ddl.auto":"create-drop",
"hibernate.show_sql":true ,
"hibernate.dialect":"org.hibernate.dialect.O racle10gDialect" ]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太明白这个问题,它是什么应用程序用户,为什么没有多个应用程序,每个应用程序只有一个用户(数据库用户)....
Grails 确实需要一个数据源来启动...
或者应用程序用户是这样的人正在登录应用程序(多租户)
http://www.grails.org/plugin/multi-tenant
I do not really understand the question, what it's an application user, why do not have multiple applications with one single user each (database user)....
Grails does need a DataSource to startup...
Or an application user is somebody that is logging in the app (Multitenancy)
http://www.grails.org/plugin/multi-tenant