2011-02-17 92 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) 
} 
相關問題