2013-02-13 80 views
0

任何人都可以知道如何將字符串作爲參數傳遞給grailsApplication.config。將字符串傳遞給服務層中的grailsApplication.config

I have tried the following. 

class ConfigPropertiesLoader { 
    def grailsApplication 

    def getProperty(String property){ 
      def properties=grailsApplication.config.${property} 

    return properties; 
} 

這是我得到的錯誤,

Error 2013-02-13 12:59:29,850 [http-bio-8080-exec-10] ERROR 0].[grails] - Servlet.service() for servlet grails threw exception 
    Message: Cannot get property 'config' on null object 
    Line | Method 
    ->> 30 | getProperty  in com.nagra.ms.sam.config.ConfigPropertiesLoader$$ENxJABt5 

    |  51 | <init>   in com.nagra.ms.sam.entity.account.controller.AccountController 
    | 196 | getBean . . . . in grails.plugin.cache.web.filter.AbstractFilter 
    | 842 | lookupController in grails.plugin.cache.web.filter.PageFragmentCachingFilter 
    | 176 | doFilter . . . . in  '' 
    |  63 | doFilter   in grails.plugin.cache.web.filter.AbstractFilter 
    | 1110 | runWorker . . . in java.util.concurrent.ThreadPoolExecutor 
    | 603 | run    in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 722 | run . . . . . . in java.lang.Thread 
+0

如果我的回答對你起作用。我很樂意接受它;) – uchamp 2016-05-03 07:06:20

回答

3

我不知道我理解你的問題相關的,所以我要列出幾個不同的方案爲您提供的解決方案:

考慮在Config.groovy中的以下兩項

feedbackAddress="[email protected]" 

blogs = {groupName-> 
     if(groupName == 'IT') { 
      //return blog URLs for IT folks 
     } else if(groupName == 'UX') { 
      //return blog URLs for UX folks 
     } else { 
      //return others 
     } 
    } 

案例1:使用字符串作爲配置n AME。您可以訪問feedbackAddress,如:

def feedbackAddress = grailsApplication.config["feedbackAddress"] 

案例2:將參數傳遞給配置。可以列出爲特定羣體的博客,如:

def groupName = "IT" 
grailsApplication.config.blogs(groupName) //this will give you whatever's defined in the IT block of config. 

希望這有助於..