在我的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的引用?
由于
答案 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)
}