0

我正在为 grails 框架开发一个支付插件。我正在使用一个支付提供商,它给了我一个 SOAP API (WSDL),我需要一个 cxf-client 来与 web 服务进行通信。

我在我的 grails 插件项目(2.2)中安装了https://github.com/ctoestreich/cxf-client(cxf-client 插件),并希望使用我在 grails 服务中添加到我的 config.groovy 的 cxf-client。

在我刚刚添加的服务类中

RecurringPortType recurringPaymentClient

我不直接启动插件项目,而是将它包含在我的主项目中,在那里我使用插件服务的一些方法(也自动连接到我的主项目中)。

使用自动装配插件服务(有效)后,我使用在插件服务类中使用自动装配 cxf-client 的方法得到一个空指针异常。cxf-client bean reuccringPaymentClient 为空。

但为什么?我是否必须将 cxf-client 配置也包含到我的 mainprojects config.groovy 中?或者是否有我的主项目可以合并或也使用我的新插件的 config.groovy 的解决方案?此时 cxf-configuration 被放在 plugins config.groovy 中——也许这就是问题所在?

使用

RecurringPortType recurringPaymentClient = ApplicationHolder.application.mainContext.getBean("recurringPaymentClient")

如 cxf-client 文档中所述,没有帮助。

4

1 回答 1

1

The Config.groovy file in a plugin only applies when the plugin is run as a standalone application itself. It is not read when the plugin is used in another application. A trick I've seen some plugins use (and which I have stolen for one of my own plugins) is to manipulate the configuration in the plugin descriptor's doWithSpring, which is usually early enough to have the required effect. In your case you'd have to make your plugin loadBefore the cxf-client plugin to ensure that your doWithSpring (creating the config) happens before that of cxf-client (which is where the settings will be used).

class MyCleverGrailsPlugin {
  def version = "0.1-SNAPSHOT"

  def loadBefore = ['cxfClient']

  def doWithSpring = {
    application.config.cxf.client.recurringPaymentClient.clientInterface = com.example.MyClientPortType
    // etc. etc.
  }
}

Or you can use ConfigSlurper by hand

def doWithSpring = {
  ConfigObject pluginConf = new ConfigSlurper(Environment.current.name).parse(com.example.MyPluginDefaultConfig)
  application.config.cxf = pluginConf.cxf.merge(application.config.cxf)
}

This loads a Config.groovy-style script from the plugin's src/groovy/com/example/MyPluginDefaultConfig.groovy, and merges the cxf section of that config into the cxf section of the main application's configuration, with settings in the app overriding the defaults supplied by the plugin.

于 2013-03-12T11:41:58.653 回答