【问题标题】:grails spring bean definitiongrails spring bean定义
【发布时间】:2011-06-29 02:58:01
【问题描述】:

在我的 Grails 插件中,我定义了以下 Spring bean

def doWithSpring = {

    // define a bean of type ConfigObjectHelper
    configHelper(ConfigObjectHelper)

    // Call a method of the bean we've just defined
    String appName = configHelper.getAsString('ftp.general.appName')

    // define another bean and pass appName to it's constructor
    pkApplication(Application, appName)
}

当我调用 configHelper.getAsString 时,我得到一个 NullPointerException,因为 configHelper 没有引用我在上一行中创建的 bean。相反,Grails 会使用该名称查找当前类的属性/字段。由于不存在,我得到一个 NullPointerException。

有没有办法在 doWithSpring 闭包中获取对 Spring bean 的引用?

谢谢

【问题讨论】:

  • 难道不能在 doWithApplicationContext 闭包中执行 pkApplication.appName = configHelper.getAsString(...) 吗?在那个阶段,doWithSpring 中定义的所有 bean 都被实例化了。

标签: spring grails groovy grails-plugin


【解决方案1】:

MethodInvokingFactoryBean 来救援!

def doWithSpring = {

    // define a bean of type ConfigObjectHelper
    configHelper(ConfigObjectHelper)    

    appName(org.springframework.beans.factory.config.MethodInvokingFactoryBean) {
        targetObject = ref('configHelper')
        targetMethod = 'getAsString'
        arguments = ['ftp.general.appName']
    }               

    // define another bean and pass appName to it's constructor
    pkApplication(Application, appName)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多