Vaadin Java EE (No XML) Integration 📎
- Create a Java EE 6 Maven 3 War Project
- Create
beans.xml
with the content <beans/> and put it into [WAR]/WEB-INF folder - Add a single dependency to vaadin:
<dependency> <groupId>com.vaadin</groupId> <artifactId>vaadin</artifactId> <version>6.8.1</version> </dependency>
-
Create a qualifier:
@Qualifier @Retention(RUNTIME) @Target({FIELD, TYPE}) public @interface VaadinApplication {}
-
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 extends Application> getApplicationClass() throws ClassNotFoundException { return application.getClass(); } @Override protected Application getNewApplication(HttpServletRequest request) throws ServletException { return application; } }
-
Create an application as
@SessionScoped
managed bean annotated with theVaadinApplication
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); } } -
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