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


