Integrating Vaadin with Guice 2.0

Introduction - Scope and Purpose #

This article shows how to use Guice 2.0 in Vaadin. Guice is a dependency injection framework from Google and available under the Apache License 2.0. It is assumend that basic knowledge about Guice is known. In particular, the documentation of the servlet extension of Guice should be known to understand the following example.

Guice setup #

Add the Guice framework jar files to your project. If you are using Maven to manage your project dependencies for more information about how to use integrate Vaadin with Maven2, refer to Maven Integration) add those entries to your 'pom.xml':

#!xml
<dependencies>
  ...
  <dependency>
    <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>3.0</version>
  </dependency>
</dependencies>

If you are not using Maven2, just add all the relevant jar files to your project: Download Guice 3.0 from the project web site. For the basic integration you will need the following libraries: - guice-3.0.jar - guice-servlet-3.0.jar - aopalliance.jar - javax.inject.jar Copy these libraries to the WebContent/WEB-INF/lib/ folder.

Guice Managed Application Servlet #

Guice will relief Vaadin from the burden to construct new Applications:

#!java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;

@Singleton
public class GuiceApplicationServlet extends AbstractApplicationServlet {

	protected final Provider<Application> applicationProvider;

	@Inject
	public GuiceApplicationServlet(Provider<Application> applicationProvider) {
		this.applicationProvider = applicationProvider;
	}

	@Override
	protected Class getApplicationClass() throws ClassNotFoundException {
		return Application.class;
	}

	@Override
	protected Application getNewApplication(HttpServletRequest request) throws ServletException {
		return applicationProvider.get();
	}

}
Here, the Provider constructs a new Application instance for each user. Copy and paste this class to your project. This class does not need to be changed.

Simple Example #

In the following, a simple custom Application is build and integrated with Guice. The custom MyApplication is the following:

#!java
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.vaadin.Application;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class MyApplication extends Application {
	
	@Inject @Named("welcome") protected String text;
	
	@Override
	public void init() {
		Window window = new Window();
		window.addComponent(new Label(text));
		
		setMainWindow(window);
	}

}

A Guice specific listener class is required to configure the Modules, i.e., the Servlet class, the Application implementation, and all other Guice-managed entities like, here, the welcome text:

#!java
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.name.Names;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.google.inject.servlet.ServletScopes;
import com.vaadin.Application;

public class MyServletConfig extends GuiceServletContextListener {

	@Override
	protected Injector getInjector() {

		ServletModule module = new ServletModule() {
			@Override
			protected void configureServlets() {
				serve("/*").with(GuiceApplicationServlet.class);

				bind(Application.class).to(MyApplication.class).in(ServletScopes.SESSION);
				bindConstant().annotatedWith(Names.named("welcome")).to("This is my first Vaadin/Guice Application");
			}
		};

		Injector injector = Guice.createInjector(module);

		return injector;
	}
}

Since the Servlet is now Guice-managed, the web.xml has to be adapted to use the configuration of the MyServletConfig class:

#!xml
<web-app>

  ...

  <filter>
     <filter-name>guiceFilter</filter-name>
     <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
  </filter>

  <filter-mapping>
     <filter-name>guiceFilter</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>
    
  <listener>
     <listener-class>de.timedout.vaadin.guice.MyServletConfig</listener-class>
  </listener>
  
</web-app>

Now, everything is done and you should be able to enjoy your first Guice/Vaadin Application.

0 Attachments
11671 Views
Average (4 Votes)
Comments