【问题标题】:Grails: How to use JNDI datasource in resources.groovy file.Grails:如何在 resources.groovy 文件中使用 JNDI 数据源。
【发布时间】:2017-09-01 14:53:11
【问题描述】:
我在 resources.groovy 文件中定义了 Spring JDBC 模板,如下所示:
jdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) {
dataSource = ? // Here I need get the datasource from JNDI
}
我需要从 JNDI 中获取数据源并将其注入到 spring 模板的数据源属性中。
PS:Grails 版本:2.4.4
请帮忙。提前致谢。
【问题讨论】:
标签:
spring
grails
groovy
jndi
【解决方案1】:
您是否有理由需要使用 jdbcTemplate?如果你在 DataSource.groovy 中设置你的数据源,你可以在那里使用 JNDI。例如:
environments {
development {
dataSource {
dbCreate = false // one of 'create', 'create-drop','update'
dialect = org.hibernate.dialect...
jndiName = "java:comp/env/jdbc/YourDataSource"
}
您可能还需要在 src/templates/war/web.xml 中添加资源引用:
<resource-ref>
<res-ref-name>jdbc/YourDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
定义了 dataSource 后,resources.groovy 中的 jdbcTemplate 应该可能会自动连接它,或者如果没有,则制作它:
jdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) {
dataSource = ref('dataSource')
}
希望有帮助!