繁体   English   中英

如何从groovy类访问grailsApplication.config?

[英]How to access grailsApplication.config from a groovy class?

我正在使用Grails 1.2.1。 如何访问我的grailsApplication.config变量? 我有这堂课......

public class Utility {
    def grailsApplication
...
    public static boolean isAuthorizedHost(String hostIpAddr) {
        // Simple validation
        if (hostIpAddr == null || hostIpAddr.length() == 0)
            return false;
        //
        def allowedDomains = grailsApplication.config.proxy.allowedDomains

但是我得到了这个编译错误......

 [groovyc] /Users/davea/Documents/workspace-sts-2.6.0.SR1/socialmediaproxy/src/groovy/com/nna/tool/proxy/Utility/Utility.groovy: 26: Apparent variable 'grailsApplication' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
  [groovyc] You attempted to reference a variable in the binding or an instance variable from a static context.
  [groovyc] You misspelled a classname or statically imported field. Please check the spelling.
  [groovyc] You attempted to use a method 'grailsApplication' but left out brackets in a place not allowed by the grammar.
  [groovyc]  @ line 26, column 24.
  [groovyc]         def allowedDomains = grailsApplication.config.proxy.allowedDomains

谢谢你的任何建议, - 戴夫

grailsApplication实例的自动注入仅适用于Spring管理的bean(Grails grailsApplication和在resource.groovy中声明的bean)。

要从其他类读取配置值,可以使用以下方法:

def config = org.codehaus.groovy.grails.commons.ConfigurationHolder.config
def allowedDomains = config.proxy.allowedDomains
import grails.util.Holders

def grailsApplication = Holders.grailsApplication

另一个答案是折旧。 (在Grails 2.5.0上测试答案)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM