【问题标题】:Where can I define the Input for grails.util.Holders.config in grails 4.0.1我在哪里可以在 grails 4.0.1 中定义 grails.util.Holders.config 的输入
【发布时间】:2020-04-30 13:58:14
【问题描述】:

在 grails 2.0 中,我可以在 Config.groovy 中定义列表并通过 grails.util.Holders.config 访问它们。

grails 4.0.1 中对应的特性是什么?

【问题讨论】:

    标签: grails


    【解决方案1】:

    查看配置类:https://docs.grails.org/4.0.1/api/grails/config/Config.html

    根据官方文档:https://docs.grails.org/latest/guide/conf.html#environments

    Grails 支持按环境配置的概念。这 application.ymlapplication.groovy 文件在 grails-app/conf 目录可以使用 YAML 或 ConfigSlurper 提供的语法。

    【讨论】:

    • 对不起,我不懂 .yml 语法。在配置中。 groovy of grails 2 我简单地写了一行 kommunikationsart=['Festnetz','Handy','Fax','eMail','Homepage'] 并通过 Holders.config.kommunikationsart 将其作为我的域类中的列表。我现在的简单问题是:对应的 .yml 行是什么以及如何从我的域类访问它?
    【解决方案2】:

    只需在 .yml 末尾添加 2 行

    registrierungsstati: [beantragt, in Prüfung, anerkannt] 
    qualitätsnachweise:[Techniker,Bachelor,Master,Diplom,Promotion] 

    并将它们作为我的域类中的列表检索

    List getQualitätsnachweise () { 
        GrailsApplication grailsApplication
        grailsApplication.config.getProperty('qualitätsnachweise')
    }
    
    List getRegistrierungsstati () {
        GrailsApplication grailsApplication
        grailsApplication.config.getProperty('registrierungsstati')
    }
    

    另请参阅 https://objectcomputing.com/news/2016/08/31/retrieving-config-values-grails-3

    【讨论】:

    • 这是个坏主意。代码的写法,grailsApplication.config 会导致NullPointerException,因为你还没有初始化本地的GrailsApplication grailsApplication 变量。即使您解决了这个问题,这也不是一个好方法。更好的办法是让 DI 容器将配置值注入到您的类中,而不是让您的类伸出手来获取配置值。
    【解决方案3】:

    更正:

    List getQualitätsnachweise () { 
    GrailsApplication grailsApplication = Holders.grailsApplication
    grailsApplication.config.getProperty('qualitätsnachweise')
    

    }

    List getRegistrierungsstati () {
    GrailsApplication grailsApplication = Holders.grailsApplication
    grailsApplication.config.getProperty('registrierungsstati')
    

    }

    grailsApplication 在这两种情况下都必须使用 Holders.grailsApplication 进行初始化,同样的错误也出现在 https://objectcomputing.com/news/2016/08/31/retrieving-config-values-grails-3

    顺便说一句,在域类中使用getter 来生成List 不是一个好主意,因为hibernate 会遇到麻烦,因为hibernate 将getter 作为数据库字段。所以我用了

    static List qualitaetsnachweise ()
    

    而不是

    静态列表getQualitaetsnachweise()

    并且要小心: grailsApplication.config.getProperty('qualitaetsnachweise') 不是作为列表出现的,你会得到它作为一个逗号分隔的字符串,你必须转换为

    ... .split(',').toList()

    【讨论】:

    • objectcomputing.com/news/2016/08/31/… 中也有同样的错误” - 我无法使用我在那篇文章中所写的方法生成错误。看起来它按预期工作。
    • static List qualitaetsnachweise () - 你真的不应该在域类中使用static 方法来检索配置值。没有任何用例会是最好的方法。
    • "请注意:grailsApplication.config.getProperty('qualitaetsnachweise') 没有作为列表出现.." - 如果你真的想引用config.getProperty,你应该使用重载版本接受一个类型作为参数。例如,grailsApplication.config.getProperty('qualitaetsnachweise', List, [])
    【解决方案4】:

    在 grails 2.0 中,我可以在 Config.groovy 中定义列表并访问它们 通过 grails.util.Holders.config。

    最好的答案取决于了解有关从何处访问值的上下文,但一般来说,您不应该使用Holders.config(在 Grails 2、3 和 4 中确实如此)。您的一种选择是让 DI 容器为您注入 List 的值。

    https://github.com/jeffbrown/user3647093configlist查看项目。

    https://github.com/jeffbrown/user3647093configlist/blob/7078c5edc896afd6afb0280f126794730485c564/grails-app/conf/application.yml#L1-L7

    ---
    music:
        drummers:
            - Neil Peart
            - Mike Portnoy
            - Bill Bruford
            - Carl Palmer
    

    https://github.com/jeffbrown/user3647093configlist/blob/7078c5edc896afd6afb0280f126794730485c564/grails-app/init/user3647093configlist/BootStrap.groovy

    package user3647093configlist
    
    import org.springframework.beans.factory.annotation.Value
    
    class BootStrap {
    
        @Value('${music.drummers}')
        List<String> drummerNames
    
        def init = { servletContext ->
            println 'Drummers:'
            for(String name : drummerNames) {
                println "\t$name"
            }
        }
    
        def destroy = {
        }
    }
    

    在应用程序启动时输出以下内容:

    Drummers:
            Neil Peart
            Mike Portnoy
            Bill Bruford
            Carl Palmer
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多