adam bien's blog

Vaadin Java EE (No XML) Integration 📎

  1. Create a Java EE 6 Maven 3 War Project
  2. Create beans.xml with the content <beans/> and put it into [WAR]/WEB-INF folder
  3. Add a single dependency to vaadin:

    <dependency>
    <groupId>com.vaadin</groupId>
    	<artifactId>vaadin</artifactId>
    	<version>6.8.1</version>
    </dependency>
    

  4. Create a qualifier:

    
    @Qualifier
    @Retention(RUNTIME)
    @Target({FIELD, TYPE})
    public @interface VaadinApplication {}
    
    
  5. Create a servlet and inject the Vaadin application. The JavaEEIntegrationServlet servlet is reusable:

    import com.vaadin.Application;
    import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
    import javax.inject.Inject;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServletRequest;
    
    @WebServlet(urlPatterns = "/*")
    public class JavaEEIntegrationServlet extends AbstractApplicationServlet {
    
        @Inject @VaadinApplication
        Application application;
    
        @Override
        protected Class getApplicationClass() throws ClassNotFoundException {
            return application.getClass();
        }
        @Override
        protected Application getNewApplication(HttpServletRequest request) throws ServletException {
            return application;
        }
    }
    
    
  6. Create an application as @SessionScoped managed bean annotated with the VaadinApplication qualifier:
    
    import com.abien.vaadin.helloapp.greetings.boundary.Greetings;
    import com.vaadin.Application;
    import com.vaadin.ui.*;
    import javax.enterprise.context.SessionScoped;
    import javax.enterprise.event.Event;
    import javax.inject.Inject;
    
    
    @SessionScoped 
    @VaadinApplication
    public class HelloApp extends Application {
    
        @Inject
        Greetings greetingService;
    
        @Inject
        Event buttonEvents;
        
        @Override
        public void init() {
            VerticalLayout layout = new VerticalLayout();
            layout.setMargin(true);
    		//...
            Window mainWindow = new Window("Vaadin 6.8 - Java EE Integration", layout);
            setMainWindow(mainWindow);
        }
    }
    
    
  7. Now EJB 3 and CDI injection should work as expected: @Stateless public class Greetings { @Inject TimeProvider provider; public String sayHello(String name) { return "Hello " + name + " at: " + provider.getCurrentTime(); } } public class TimeProvider { public long getCurrentTime(){ return System.currentTimeMillis(); } }

The Maven 3 project was pushed to https://github.com/AdamBien/vaadin-javaee