+-

在我的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的引用?
谢谢
最佳答案
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)
}
点击查看更多相关文章
转载注明原文:Grails Spring bean定义 - 乐贴网