grails spring bean的定义

时间:2011-02-17 13:50:19

标签: spring grails groovy grails-plugin

在我的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的引用?

由于

1 个答案:

答案 0 :(得分:4)

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)
}