Introduction-ScopeandPurpose#
This article will show you how to use Spring in a web application relying on Vaadin.
It is assumed that the readers have a basic knowledge of the Spring Framework. For more information about Spring, refer to theofficial web site or to the excellent bookSpring in Action, by Craig Walls.
Springsetup#
Add the Spring framework jar files to your project. If you are using Maven to manage your project dependencies (and you should ;) for more information about how to use integrate Vaadin with Maven2, refer toMaven Integration) add those entries to your'pom.xml':
#!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:
#!xml
<web-app>
..
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
And add the relevant information about your application context files :
#!xml
<web-app>
..
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
</web-app>
SpringContextHelperclass#
To access your Spring managed beans, you will need a helper class capable of using your Vaadin Application class to get the relevant session information, and thus the Spring context:
#!java
import com.vaadin.Application;
import com.vaadin.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);
}
}
SimpleUsageExample#
A very basic example, where you get a bean managed in Spring in the Vaadin Application:
#!java
public class SpringHelloWorld extends com.vaadin.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() ));
}
}
MoreSimpleExamplewithSpring2.5.x#
You could also use annotations. Make sure AspectJ is configured. Then spring aspectAnnotationBeanConfigurerAspect do the job for you.
#!java
@Configurable(preConstruction = true)
public class SpringHelloWorld extends com.vaadin.Application {
@Autowired
private MyBeanInterface bean;
public void init() {
final Window main = new Window("Hello window");
setMainWindow(main);
main.addComponent(new Label( bean.myMethod() ));
}
}
Spring context xml:
#!xml
<!-- Turn on AspectJ @Configurable support -->
<context:spring-configured />
<context:component-scan base-package="foo" />
<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
Maven2 pom.xml:
#!xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>2.5.5</version>
</dependency>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.3</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
That's all now Spring should work nicely with Vaadin. You can test your application for example with mvn tomcat:run or mvn jetty:run command.