2017-04-25 85 views
0

在Grails 2中,我們使用內部庫來動態設置Config.groovy文件中的屬性值。該庫查看特定於環境的配置管理數據庫並填充這些值。這是在Grails的2Grails3 application.groovy動態屬性

import com.mycompany.otherproject.ConfigurationManagement 

family { 
    dynamic.property = ConfigurationManagement.getValue("dynamic.property.name", "default") 
    static.proprty = "static value" 
} 

基礎上Upgrading from Grails 2.x - Reference Documentation工作的配置的一個例子,我複製此信息application.groovy,但現在我碰到下面的錯誤,我一直未能解決。

Error | 
Error occurred running Grails CLI: startup failed: 
script14930638938641309394273.groovy: 1: unable to resolve class com.mycompany.otherproject.ConfigurationManagement 
@ line 1, column 1. 
    import com.mycompany.otherproject.ConfigurationManagement 
^

建議?

+0

您是否嘗試不導入類,而是調用'com.mycompany.otherproject.ConfigurationManagement'或它的實例中的方法?你確定類是存在於你的類路徑中嗎? –

+0

我正在使用IntelliJ Idea,如果我使用導入,那麼我可以跳轉到方法定義。如果我刪除導入,那麼我不能再這樣做,並且構建會給我一個不同的錯誤:運行Grails CLI時發生錯誤:沒有方法簽名:groovy.util.ConfigObject.getValue()適用於參數類型:(java .lang.String,java.lang.String) – GeoGriffin

+0

也許我沒有運行這個權利。我從我的運行配置中執行「grails clean」,就像我在Grails 2中所做的那樣,但我不認爲這是正確的。我較早前完成了這個任務,並且完成了構建步驟,但是我只是爲「gradle clean」和「gradle build」設置了一個新的運行配置,現在我得到了與某些單元測試有關的不同錯誤以及我需要的其他重構。 – GeoGriffin

回答

0

事實證明,您不能直接設置屬性,但可以聲明一個局部變量,然後使用該值來設置屬性。

import com.mycompany.otherproject.ConfigurationManagement 

def dynamicProperty = ConfigurationManagement.getValue("dynamic.property.name", "default") 

family { 
    dynamic.property = dynamicProprty 
    static.proprty = "static value" 
} 
+0

就像我們在application.groovy中沒有這樣的配置項目,但我們在我們的runtime.groovy中有這樣的配置項目,並且它們的工作原理與您的原始示例完全一樣(import line,then設置該屬性等於某個靜態方法調用的返回值)。但是,我們對該方法的調用不在任何配置項目塊中。我認爲這可能是不同的,因爲我看到你的dynamicProperty也設置在族{...}區塊之外。 – Daniel

+0

正確。它絕對不能在塊中設置。 – GeoGriffin

+1

我現在正在升級一個應用程序,所以在這裏留下評論給其他未來碰到這個的人。我有一個字符串屬性在另一個屬性塊內,它的工作是將所有東西包裝在$ {...}中。所以GeoGriffin的第一個例子不起作用(正如他所說的),但是解決方法包括:在塊外部設置值(像他展示的那樣),或者做一些類似於name =「$ {YourClass.getProperty()}」的事情。 – Daniel