Grails 3 PluginDescriptor 配置

Grails 3 PluginDescriptor configuration

我正在将现有的 Grails 2.5.6 项目转换为 Grails 3.3.11。

对于我现有的应用程序 (Grails 2.5.6),插件描述符具有如下代码:

def doWithApplicationContext = { applicationContext ->
    def config = applicationContext.grailsApplication.config
    def key = config.property.key
    key.put(Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"])
}

此代码适用于早期版本的 grails。但是在我升级到 grails 3.3.11 之后它会抛出异常: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

这是在行:

key.put(Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"])

查看密钥类型后,即 config.property.key,它显示类型为 org.grails.config.NavigableMap$NullSafeNavigator

旧版本是LinkedHashMap。 property.key 在 application.groovy 上设置很好 /grails-app/conf/application。groovy property.key = [:]

我也试过将插件描述符中的 property.key 类型设置为 java.util.HashMap 外部。不过好像没有采用新的类型。

我这里做错了什么?

而不是尝试动态地这样做:

def doWithApplicationContext = { applicationContext ->
    def config = applicationContext.grailsApplication.config
    def key = config.property.key
    key.put(2, [controller: "results", action: "showData", templatePath: "/results/data"]) 
}

您可以像这样在 grails-app/conf/plugin.yml 中定义这些值:

---
property:
  key:
    '2':
      controller: results
      action: showData
      templatePath: '/results/data`

编辑

问题已更改,上述内容不再有效。

而不是这样做:

def config = applicationContext.grailsApplication.config
def key = config.property.key
key.put(Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"])

您可以将其简化为:

config.merge([property: [key: [Constants.RESULT_CONST, [controller: "results", action: "showData", templatePath: "/results/data"]]]])

感谢@Jeff 的意见和建议。

这是设置配置参数并在控制器或任何其他 grails 组件中检索它们的合并代码。

插件描述符:

class ResultGrailsPlugin extends Plugin {
   void doWithApplicationContext() { 
      config.merge(['property': ['key': ["${Constants.RESULT_CONST}": [controller: "results", action: "showData", templatePath: "/results/data"]]]])
   }
}

控制器:

class ResultController {
   def index() {
      def resultConfigMap = grailsApplication.config.get('property.key.' + Constants.RESULT_CONST)
      ...
   }
}