Friday, September 3, 2010

Playing with Vaadin + Spring

A few weeks ago, I stumbled on Vaadin Framework and it came with a catchy tag-line:
Vaadin is a Java Framework for building modern web applications that looks great, perform well, make you and your users happy.
That got my attention and so like a kid with a new toy, I started playing around with it and try to do stuff with it. It took me a while, read a few blogs but I have managed to get the hang of Vaadin and even got it to work with Hibernate and Spring. Vaadin with Hibernate was easy to figure out but Vaadin, Spring and Hibernate along with a DAO was a bit of a bitch of figure out but I eventually did. Nicolas Fränkel's blog gave me new insight on how to approach the problem. I sort of agree with his opinions on how to get Vaadin to work with Spring as compared to the techniques posted in the Vaadin wiki page. To quote Nicolas:
The first one uses the Helper “pattern”, a class with static method that has access to the Spring application context. IMHO, those Helper classes should be forgotten now we have DI since they completely defeat its purpose. If you need to explicitly call the Helper static method in order to get the bean, where’s the Inversion of Control?
The second solution uses Spring proprietary annotation @Autowired in order to use DI. Since IoC is all about decoupling, I’m vehemently opposed to coupling my code to the Spring framework.
 The trick was understanding how to isolate the Spring Context from the Vaadin application. With them separated, all that was needed was configuration files.

The thing was not to forget to configure the listener in web.xml file:
     <!-- <param-value>classpath*:WebApplicationContext.xml</param-value> -->  

After that the WebApplicationContext.xml file in the WEB-INF folder.
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>  
 <beans xmlns="" xmlns:aop=""  
      xmlns:context="" xmlns:tx=""  
      <context:property-placeholder location="classpath*:*.properties" />  
     <context:annotation-config />  
     <!-- add autowired or explicitly configured beans here -->  
     <bean id="myVaadinApplication" class=""/>  

With that, you can start doing some really interesting stuff!