在 Grails 3 下,我采纳了 Jeff Scott Brown 的建议,改用 GrailsApplicationAware:
这是设置配置 bean 的方式:
因此,在您的新插件描述符中,您需要将 grails 2 样式 def doWithSpring 更改为 ClosureDoWithSpring,如下所示:
注意在 Grails 2 中我们注入了 grailsApplication,在 grails 3 中我们所做的只是声明 bean:
/*
def doWithSpring = {
sshConfig(SshConfig) {
grailsApplication = ref('grailsApplication')
}
}
*/
Closure doWithSpring() { {->
sshConfig(SshConfig)
}
}
现在获取您的插件配置:
src/main/groovy/grails/plugin/remotessh/SshConfigSshConfig.groovy
package grails.plugin.remotessh
import grails.core.GrailsApplication
import grails.core.support.GrailsApplicationAware
class SshConfig implements GrailsApplicationAware {
GrailsApplication grailsApplication
public ConfigObject getConfig() {
return grailsApplication.config.remotessh ?: ''
}
}
grails.plugin.remotessh.RemoteSsh.groovy:
String Result(SshConfig ac) throws InterruptedException {
Object sshuser = ac.config.USER ?: ''
Object sshpass = ac.config.PASS ?: ''
...
现在这是你的配置对象被传递到你的 src groovy 类。最终用户应用程序将像这样传入 sshConfig bean:
class TestController {
def sshConfig
def index() {
RemoteSSH rsh = new RemoteSSH()
....
def g = rsh.Result(sshConfig)
}
编辑添加,刚刚发现这个:)这是相关或重复的问题:
http://grails.1312388.n4.nabble.com/Getting-application-config-in-doWithSpring-closure-with-a-Grails-3-application-td4659165.html