2017-04-25 80 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