X

Geertjan's Blog

  • September 28, 2008

Spring's MethodInvokingFactoryBean & Griffon

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 4 )
  • Mark Fisher Sunday, September 28, 2008

    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


  • thierry Monday, September 29, 2008

    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


  • Geertjan Monday, September 29, 2008

    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.


  • guest Monday, May 25, 2015

    Thanks, nice post


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.