如何在 Grails 3 中访问普通 groovy 类 中的数据源?
How do I access datasources in ordinary groovy classes in Grails 3?
我正在尝试从普通 Groovy class(无服务)中 运行 sql-编码。
在 Grails 2 中,我可以通过这样做来访问数据源:
public GroovyClass() {
def ctx = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
def dataSource = ctx.getBean('dataSource')
sql = new Sql(dataSource)
}
迁移到 Grails 3.3.8 后,代码不再有效。 Grails 3.3.8 中正确的方法是什么?
我刚刚测试了这个,它似乎提供了数据源
def ds = Holders.grailsApplication.mainContext.getBean('dataSource')
println "DataSource: ${ds}" ---> DataSource: org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy@5e91ade8
没有尝试对其进行任何操作,但看起来是正确的。
"Hollywood Principle" 表示 "Don't call us, we will call you" 这就是依赖注入的意义所在。在你的情况下,不要去拿 dataSource
,让 dataSource
给你。
有多种方法可以做到这一点。在 https://github.com/jeffbrown/asdudemo.
查看项目
// src/main/groovy/ascudemo/FirstGroovyClass.groovy
package ascudemo.helpers
import groovy.sql.Sql
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.InitializingBean
import javax.sql.DataSource
@Slf4j
class FirstGroovyClass implements InitializingBean {
DataSource dataSource
Sql sql
void logSomeInfo() {
// both have been initialized
log.debug "dataSource: ${dataSource}"
log.debug "sql: ${sql}"
}
@Override
void afterPropertiesSet() throws Exception {
sql = new Sql(dataSource)
}
}
// src/main/groovy/ascudemo/SecondGroovyClass.groovy
package ascudemo.helpers
import groovy.sql.Sql
import groovy.util.logging.Slf4j
import javax.annotation.PostConstruct
import javax.sql.DataSource
@Slf4j
class SecondGroovyClass {
DataSource dataSource
Sql sql
void logSomeInfo() {
// both have been initialized
log.debug "dataSource: ${dataSource}"
log.debug "sql: ${sql}"
}
@PostConstruct
void initSql() throws Exception {
sql = new Sql(dataSource)
}
}
// src/main/groovy/ascudemo/SecondGroovyClass.groovy
package ascudemo.helpers
import groovy.sql.Sql
import groovy.util.logging.Slf4j
import org.springframework.beans.BeansException
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import javax.sql.DataSource
@Slf4j
class ThirdGroovyClass implements ApplicationContextAware {
Sql sql
void logSomeInfo() {
// sql been initialized
log.debug "sql: ${sql}"
}
@Override
void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
DataSource dataSource = applicationContext.getBean('dataSource', DataSource)
sql = new Sql(dataSource)
}
}
// grails-app/controllers/ascudemo/DemoController.groovy
package ascudemo
class DemoController {
SomeService someService
def index() {
someService.logSomeInfo()
render 'Success'
}
}
https://github.com/jeffbrown/asdudemo/blob/master/grails-app/services/ascudemo/SomeService.groovy
// grails-app/services/ascudemo/SomeService.groovy
package ascudemo
import ascudemo.helpers.FirstGroovyClass
import ascudemo.helpers.SecondGroovyClass
import ascudemo.helpers.ThirdGroovyClass
class SomeService {
FirstGroovyClass firstGroovyBean
SecondGroovyClass secondGroovyBean
ThirdGroovyClass thirdGroovyBean
def logSomeInfo() {
firstGroovyBean.logSomeInfo()
secondGroovyBean.logSomeInfo()
thirdGroovyBean.logSomeInfo()
}
}
https://github.com/jeffbrown/asdudemo/blob/master/grails-app/conf/spring/resources.groovy
// grails-app/conf/spring/resources.groovy
import ascudemo.helpers.FirstGroovyClass
import ascudemo.helpers.SecondGroovyClass
import ascudemo.helpers.ThirdGroovyClass
beans = {
// demonstrates one approach
firstGroovyBean(FirstGroovyClass) { bean ->
bean.autowire = 'byName'
}
// demonstrates another approach
secondGroovyBean(SecondGroovyClass) { bean ->
bean.autowire = 'byName'
}
// demonstrates a third approach
thirdGroovyBean ThirdGroovyClass
}
运行 应用程序并向 http://localhost:8080/demo/ 发送请求,这将验证所有 3 种方法是否有效。
还有其他方法可以做到这一点,但我希望以上方法之一对您有所帮助。
祝你好运!
我正在尝试从普通 Groovy class(无服务)中 运行 sql-编码。 在 Grails 2 中,我可以通过这样做来访问数据源:
public GroovyClass() {
def ctx = ServletContextHolder.servletContext.getAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT)
def dataSource = ctx.getBean('dataSource')
sql = new Sql(dataSource)
}
迁移到 Grails 3.3.8 后,代码不再有效。 Grails 3.3.8 中正确的方法是什么?
我刚刚测试了这个,它似乎提供了数据源
def ds = Holders.grailsApplication.mainContext.getBean('dataSource')
println "DataSource: ${ds}" ---> DataSource: org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy@5e91ade8
没有尝试对其进行任何操作,但看起来是正确的。
"Hollywood Principle" 表示 "Don't call us, we will call you" 这就是依赖注入的意义所在。在你的情况下,不要去拿 dataSource
,让 dataSource
给你。
有多种方法可以做到这一点。在 https://github.com/jeffbrown/asdudemo.
查看项目// src/main/groovy/ascudemo/FirstGroovyClass.groovy
package ascudemo.helpers
import groovy.sql.Sql
import groovy.util.logging.Slf4j
import org.springframework.beans.factory.InitializingBean
import javax.sql.DataSource
@Slf4j
class FirstGroovyClass implements InitializingBean {
DataSource dataSource
Sql sql
void logSomeInfo() {
// both have been initialized
log.debug "dataSource: ${dataSource}"
log.debug "sql: ${sql}"
}
@Override
void afterPropertiesSet() throws Exception {
sql = new Sql(dataSource)
}
}
// src/main/groovy/ascudemo/SecondGroovyClass.groovy
package ascudemo.helpers
import groovy.sql.Sql
import groovy.util.logging.Slf4j
import javax.annotation.PostConstruct
import javax.sql.DataSource
@Slf4j
class SecondGroovyClass {
DataSource dataSource
Sql sql
void logSomeInfo() {
// both have been initialized
log.debug "dataSource: ${dataSource}"
log.debug "sql: ${sql}"
}
@PostConstruct
void initSql() throws Exception {
sql = new Sql(dataSource)
}
}
// src/main/groovy/ascudemo/SecondGroovyClass.groovy
package ascudemo.helpers
import groovy.sql.Sql
import groovy.util.logging.Slf4j
import org.springframework.beans.BeansException
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import javax.sql.DataSource
@Slf4j
class ThirdGroovyClass implements ApplicationContextAware {
Sql sql
void logSomeInfo() {
// sql been initialized
log.debug "sql: ${sql}"
}
@Override
void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
DataSource dataSource = applicationContext.getBean('dataSource', DataSource)
sql = new Sql(dataSource)
}
}
// grails-app/controllers/ascudemo/DemoController.groovy
package ascudemo
class DemoController {
SomeService someService
def index() {
someService.logSomeInfo()
render 'Success'
}
}
https://github.com/jeffbrown/asdudemo/blob/master/grails-app/services/ascudemo/SomeService.groovy
// grails-app/services/ascudemo/SomeService.groovy
package ascudemo
import ascudemo.helpers.FirstGroovyClass
import ascudemo.helpers.SecondGroovyClass
import ascudemo.helpers.ThirdGroovyClass
class SomeService {
FirstGroovyClass firstGroovyBean
SecondGroovyClass secondGroovyBean
ThirdGroovyClass thirdGroovyBean
def logSomeInfo() {
firstGroovyBean.logSomeInfo()
secondGroovyBean.logSomeInfo()
thirdGroovyBean.logSomeInfo()
}
}
https://github.com/jeffbrown/asdudemo/blob/master/grails-app/conf/spring/resources.groovy
// grails-app/conf/spring/resources.groovy
import ascudemo.helpers.FirstGroovyClass
import ascudemo.helpers.SecondGroovyClass
import ascudemo.helpers.ThirdGroovyClass
beans = {
// demonstrates one approach
firstGroovyBean(FirstGroovyClass) { bean ->
bean.autowire = 'byName'
}
// demonstrates another approach
secondGroovyBean(SecondGroovyClass) { bean ->
bean.autowire = 'byName'
}
// demonstrates a third approach
thirdGroovyBean ThirdGroovyClass
}
运行 应用程序并向 http://localhost:8080/demo/ 发送请求,这将验证所有 3 种方法是否有效。
还有其他方法可以做到这一点,但我希望以上方法之一对您有所帮助。
祝你好运!