Download NetBeans!

20080928 Sunday September 28, 2008

Spring's MethodInvokingFactoryBean & Griffon

Here's my Spring application context:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass">
            <value>java.lang.System</value>
        </property>
        <property name="targetMethod">
            <value>getProperties</value>
        </property>
    </bean>

    <bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject">
            <ref local="sysProps"/>
        </property>
        <property name="targetMethod">
            <value>getProperty</value>
        </property>
        <property name="arguments">
            <list>
                <value>java.version</value>
            </list>
        </property>
    </bean>

</beans>

And here's my Griffon "Initialize.groovy":

import org.springframework.richclient.application.ApplicationLauncher

def appContextPath = "richclient-application-context.xml"

try {
    new ApplicationLauncher(appContextPath)
} catch (RuntimeException e) {
    println 'Failure'
}

Next, in "Startup.groovy", I initialize the controller:

def rootController = app.controllers.root
rootController.gotoPage()

And now I can get the application context and inject the Spring bean defined at the start of this blog entry:

class GriffonDemo1Controller {

    def view

    def gotoPage() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("richclient-application-context.xml")
        String jVersion = ctx.getBean("javaVersion")
        view.versionLabel.text = "Java Version: " + jVersion
    }

}

The final line above refers to my view, which is exactly this:

application(title:'GriffonDemo1',  size:[250,50], location:[50,50], locationByPlatform:true) {

    label(id:'versionLabel')

}

Having invoked the method on the Spring bean, the code above shows the result in a label in the view, which therefore now looks like this:

One improvement might be to set the application context in the model, via "Initialize.groovy", and then retrieve it in the view, so that everything is nicely centralized, instead of repeating the location/name of the application context, which is what I'm doing above.

It follows from the above that if you use Spring to create your Swing applications, you can code in Groovy and structure your sources according to the strict MVC pattern encouraged by Griffon. I also believe that this could be the basis for Spring RCP integration with Griffon, which implies docking framework support (VLDocking, JIDE, etc) for Griffon.

Sep 28 2008, 02:57:53 PM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/spring_s_methodinvokingfactorybean_griffon
Comments:

I realize it's not the main point of this post, but the spring configuration could be simplified to this:

<context:property-placeholder/>

<bean id="javaVersion" class="java.lang.String">
<constructor-arg value="${java.version}"/>
</bean>

Regards,
Mark

Posted by Mark Fisher on September 28, 2008 at 05:48 PM PDT #

Very nice blog.
Here you write about the integration of the spring RCP and Griffon but ...
I'm developping a netbeans RCP application.
What are your expectations about the integration of Nb RCP and Griffon : groovy scripting of the platform, strict MVC pattern, reuse of the wonderfull windowing api of netbeans

Thanks
Thierry

Posted by thierry on September 29, 2008 at 12:33 AM PDT #

Thanks Mark, for the help. Tried it quickly, but didn't work for me; will try again. Thierry, that's an interesting thought, i.e., creating NetBeans modules in Griffon structure and Groovy code. Will look into it after Spring RCP.

Posted by Geertjan on September 29, 2008 at 12:48 PM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed