Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails spring bean definition

In my Grails plugin I define the following Spring beans

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)
}

When I call configHelper.getAsString I get a NullPointerException, because configHelper does not refer to the bean I created in the previous line. Instead Grails looks for a property/field of the current class with this name. As none exists, I get a NullPointerException.

Is there a way to get a reference to a Spring bean within the doWithSpring closure?

Thanks

like image 577
Dónal Avatar asked Jul 23 '26 23:07

Dónal


1 Answers

MethodInvokingFactoryBean to the rescue!

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)
}
like image 133
Dónal Avatar answered Jul 25 '26 14:07

Dónal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!