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:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>classpath*:WebApplicationContext.xml</param-value> -->
<param-value>WEB-INF/WebApplicationContext.xml</param-value>
</context-param>
After that the WebApplicationContext.xml file in the WEB-INF folder.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath*:*.properties" />
<context:annotation-config />
<!-- add autowired or explicitly configured beans here -->
<bean id="myVaadinApplication" class="edu.chuybook.app.MyVaadinApplication"/>
</beans>
With that, you can start doing some really interesting stuff!
No comments:
Post a Comment