2011-02-17 90 views
3

在我的Grails插件我定义以下的Spring beansGrails中的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并不是指我在前面行创建的豆。相反,Grails使用这个名字查找当前类的属性/字段。如果不存在,我会得到一个NullPointerException。

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

感谢

+1

在doWithApplicationContext关闭中执行pkApplication.appName = configHelper.getAsString(...)是不可能的吗?在那个阶段,所有在doWithSpring中定义的bean都被实例化。 – 2011-02-18 09:39:43

回答

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) 
} 
相关问题