将@GrailsCompileStatic批注添加到方法或类中可允许使用动态GORM查找器,例如findAllByIdAndProperty()。
但是,添加注释不允许使用domainClass.withTransaction(),这也是GORM AST的补充。为什么?
(使用grails-2.5.3)
更新(16/05/16-10:25 AM)
@ jeff-scott-brown是正确的,它确实可以正常工作,因此以下是@GrailsCompileStatic失败的代码:
...
RestfulApiService service = ServiceUtils.getService(resourceName)
service.resourceClass.withTransaction { /* do something */ }
(resourceClass是Class类型)
错误:
Compilation error: startup failed:
C:\...\myfile.groovy: 100: [Static type checking] - Cannot find matching method java.lang.Class#withTransaction(groovy.lang.Closure). Please check if the declared type is right and if the method exists.
@ line 100, column 13.
service.resourceClass.withTransaction {
^
添加注释后,为什么withTransaction()在这种情况下失败?
最佳答案
Why does @GrailsCompileStatic allow calling dynamic GORM finders but not domainClass.withTransaction()?
是的https://github.com/jeffbrown/withtx/blob/master/grails-app/controllers/demo/DemoController.groovy上的代码进行编译。
import grails.compiler.GrailsCompileStatic
@GrailsCompileStatic
class DemoController {
def index() {
Person.withTransaction {
// ...
}
render 'Success!'
}
}
关于grails - 为什么@GrailsCompileStatic允许调用动态GORM查找器而不允许domainClass.withTransaction()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37126416/