【发布时间】:2021-08-20 09:48:05
【问题描述】:
我想写以下内容:
import grails.core.GrailsApplication
class MyDomainClass{
GrailsApplication grailsApplication
String otherName
String getName(){
String prop = grailsApplication.config.getProperty('my.property.from.application.yml')
return prop + otherName
}
static mapping = {
autowire true
}
}
但是,如果我在域类中有 grailsApplication,则 grails 命令grails run-app 不会启动网络服务器并永远等待。它输出以下警告,但没有错误:
> IDLE
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/Users/martin/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.5.14/f0a005fb21e7bd9b7ebf04cd2ecda0fc8f3be59d/groovy-2.5.14.jar) to method java.util.AbstractMap.clone()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/Users/martin/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.5.14/f0a005fb21e7bd9b7ebf04cd2ecda0fc8f3be59d/groovy-2.5.14.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
这是一种不好的做法还是错误?我该如何解决这个问题?在我看来,最明显的选择是调用服务。
我正在使用以下 Grails 版本:
| Grails Version: 4.0.11
| JVM Version: 11.0.11
【问题讨论】:
-
grailsApplication 中的 A 在变量定义中大写,但在使用时不大写。
-
我不知道将 grailsApplication 放在域类中是否“错误”,但这肯定是不必要的。您可以在任何地方使用
grails.util.Holders.config来访问配置,这将比注入 grailsApplication 更简单、更干净。 -
声明
grailsApplication瞬态。此外,您似乎想在static transients块中定义name属性