在我的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)
}