【问题标题】:Getting application config in doWithSpring closure with a Grails 3 application使用 Grails 3 应用程序在 doWithSpring 闭包中获取应用程序配置
【发布时间】:2015-06-19 06:27:17
【问题描述】:

Grails 3 允许作者使用类似于为 Grails 2 插件提供的启动钩子。我正在查看在 doWithSpring 闭包中定义 bean,并且我想根据一些配置值将值传递给新的 bean。但是,我不知道如何获取 grailsApplication 实例或应用程序配置。您如何使用 Grails 3 做到这一点?

【问题讨论】:

  • 我花了一段时间环顾四周,当时找不到任何插件。我最终从实际应用程序中提取配置并将配置元素传递给插件。老实说,我自己对此并不满意.. github.com/vahidhedayati/RemoteSSH/tree/grails3
  • “老实说,我自己对此并不满意。” - @vahid 请参阅下面的答案。在您的插件中,您只需引用 config 属性即可。这就是你要找的吗?

标签: grails grails-3.0


【解决方案1】:

在 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

【讨论】:

  • 我在 grails dev google 小组上开始了这个话题,因为当时这里没有太多的吸引力。只是为了上下文:)
  • 哈,是的,我将它们都绑定在这里,这样对于将来的其他人来说,痛苦会减少很多,而且同样类型的不同方法问题也会减少。都是好东西:)
【解决方案2】:

您的插件应该扩展grails.plugins.Plugin,它定义了getConfig() 方法。见https://github.com/grails/grails-core/blob/9f78cdf17e140de37cfb5de6671131df3606f2fe/grails-core/src/main/groovy/grails/plugins/Plugin.groovy#L65

您应该可以只引用config 属性。

同样,您可以引用在https://github.com/grails/grails-core/blob/9f78cdf17e140de37cfb5de6671131df3606f2fe/grails-core/src/main/groovy/grails/plugins/Plugin.groovy#L47 中定义的继承的grailsApplication 属性。

希望对你有帮助。

【讨论】:

  • 谢谢杰夫,对不起,我的意思是说我对自己的解决办法或解决办法不满意。在写这个之前应该先玩一下......想到的Q......插件描述符当然扩展了Plugin,然后尝试创建配置bean。配置类 github.com/vahidhedayati/RemoteSSH/blob/master/src/groovy/… 现在是否也应该扩展 grails.plugins.Plugin 以使其正常工作?
  • "配置类现在是否也应该扩展 grails.plugins.Plugin 以使其正常工作?" - 不。只有插件类应该扩展grails.plugins.Plugin。查看grails3 分支上的代码并不清楚您真正想要做什么,但基于master 分支上的版本,您可能想要注册SshConfig bean在doWithSpring 中,然后从那里注入配置值。我也会使用GrailsApplicationAware 而不是显式注入grailsApplication bean,但它可能根本不需要。
  • master 分支上的一些代码对我来说是荒谬的。
  • 这对于刚开始编写 Grails 3 插件的人来说是有用的信息,但是这个问题针对的是 Grails 3 应用程序,而不是插件。在grails.1312388.n4.nabble.com/… 可以找到更多详细信息。
  • @BudJB 对于应用程序,您可以在 Application 子类中覆盖 doWithSpring
猜你喜欢
  • 2015-05-16
  • 2011-03-21
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-01
  • 2016-01-30
相关资源
最近更新 更多