Integrating ITMill Toolkit with Spring
Introduction – Scope and Purpose
This article will show you how to use Spring in a web application relying on ITMill Toolkit.
It is assumed that the readers have a basic knowledge of the Spring Framework. For more informations about Spring, refer to the official web site or to the excellent book Spring in Action, by Craig Walls.
Spring setup
Add the Spring framework jar files to your project.
If you are using Maven to manage your project dependencies (and you should ;) for more informations about how to use integrate ITMill Toolkit with Maven2, refer to this article ) add those entries to your pom.xml:
<dependencies> ... <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>2.0.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>2.0.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>2.0.3</version> </dependency> </dependencies>
If you are not using Maven2, just add all the relevant jar files to your project.
Edit your web.xml to add the Spring listener:
<web-app> .. <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
And add the relevant informations about your application context files :
<web-app> .. <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> </web-app>
Spring Context Helper class
To access your Spring managed beans, you will need a helper class capable of using your ITMill Application class to get the relevant session informations, and thus the Spring context:
import com.itmill.toolkit.Application; import com.itmill.toolkit.terminal.gwt.server.WebApplicationContext; import javax.servlet.ServletContext; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; public class SpringContextHelper { private ApplicationContext context; public SpringContextHelper(Application application) { ServletContext servletContext = ((WebApplicationContext) application.getContext()).getHttpSession().getServletContext(); context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); } public Object getBean(final String beanRef) { return context.getBean(beanRef); } }
Simple Usage Example
A very basic example, where you get a bean managed in Spring in the ITMill Application:
public class SpringHelloWorld extends com.itmill.toolkit.Application { public void init() { final Window main = new Window("Hello window"); setMainWindow(main); SpringContextHelper helper = new SpringContextHelper(this); MyBeanInterface bean = (MyBeanInterface) helper.getBean("myBean"); main.addComponent(new Label( bean.myMethod() )); } }
