Adam Bien's Weblog

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


NEW workshop: Microservices with Java EE 7 and Java 8, January 26th, 2015, Airport Munich

A book about rethinking Java EE Patterns

Comments:

From the title... "No XML". Sounds good!

4th line... well f*** me if that isn't XML!!!

Posted by Neil Bartlett on August 08, 2012 at 12:25 AM CEST #

@Neil,

This post is about integration, not Java EE 6. You can simply skip the first two items -- they are only Java EE 6 related and have absolutely nothing to do with the vaadin integration. Usually you will need a Java EE 6 project first for the integration :-)

Having CDI and Java EE 6 setup, NO XML is needed to integrate vaadin.

beans.xml is only needed for CDI and not for the vaadin integration.

Now: what do you mean by "f*** me"? :-)

thanks for your interesting comment and offer!

adam

Posted by Adam Bien on August 08, 2012 at 02:56 AM CEST #

Whoa ! That is what I'm looking for, thanks for the information.
JEE is our main platform and Vaadin is in evaluation for our future RIA development.

Posted by Kenneth on August 08, 2012 at 08:10 AM CEST #

There are quite a few tricks to vaadin integration with JEE.
For example you can't realy use conversation scope you could use in jsf, and request scope could work counter intuitively (due to nature of vaadin), so you are down to application and request scoped beans.
On the other hand - you could use @Inject Instance<T> to achive injection like with lookup in netbeans platform (a feature I use extensively @work).

As for previous polite comment - you could actually use an empty beans.xml file - would you cope with .xml extension or would that be too much for you ?
And if you realy want to work with JEE - sorry - you will need xml.

Posted by psychollek on August 08, 2012 at 01:18 PM CEST #

Hi Adam!

Nice to see, that more and more people talk about Vaadin.

There is an issue with injecting the application into the servlet. Requests from different user session will block each other.

See Vaadin forum: https://vaadin.com/forum/-/message_boards/view_message/963494#_19_message_963494

Greetings
Oliver

Posted by Oliver Damm on August 08, 2012 at 10:18 PM CEST #

@Adam is there a special reason why you created a qualifier for this? As I'm beginning to work with CDI I don't see that there is a benefit from that.

Posted by Sebastian Basner on August 09, 2012 at 07:50 PM CEST #

@psychollek,

In "my" Java EE projects we only have an empty beans.xml, and a short persistence.xml. So in relation there is No XML :-)

thanks for your comment!

adam

Posted by Adam Bien on August 10, 2012 at 09:02 PM CEST #

@Oliver,

the issue is going to be fixed soon :-)

adam

Posted by Adam Bien on August 10, 2012 at 09:02 PM CEST #

@Sebastian,

" is there a special reason why you created a qualifier for this? "

Yes! Vaadin 6.8.X ships with several subclasses of the Application superclass. The qualifier is just a workaround...

thanks!

adam

Posted by Adam Bien on August 10, 2012 at 09:04 PM CEST #

alternatively, it also runs with openejb on tomcat

Posted by jack on August 21, 2012 at 03:10 PM CEST #

I found this solution clean and simple, but the synchronization issue is a blocker. For now I'm going with the CDI Utils plugin (https://vaadin.com/directory#addon/cdi-utils).

Any new thoughts on this? Is it possible to avoid this problem?

Posted by Paulo Suderio on August 27, 2012 at 01:28 AM CEST #

Hi Adam,

The lines

@Inject @VaadinApplication
Application application;

produce a Netbeans-Warning:

"No enabled eligible for injection beans are found"

I think, this is caused by the final method

private static final Logger getLogger()

in the class com.vaadin.Application.

How can I get rid of this warning?

Thanks. Markus.

Posted by Markus on August 29, 2012 at 05:22 PM CEST #

Nicely done !

However there might be a small issue with your solution if one use the restartApplication mechanism of Vaadin (clicking restart in the debug console for example).

In that case the getNewApplication() method will be called again, and because the @VaadinApplication bean is session scoped, the same instance will be returned. The init() method of the @VaadinApplication bean will then be called one more time on the same instance.

The overall effect is highly dependent on how the initialization sequence has been implemented and on if the application instance holds state. But it may break existing Vaadin application because the regular initialization sequence calls init() only once per instance.

Posted by Antoine on September 07, 2012 at 05:50 PM CEST #

There are other issues with this approach.

try adding another application object to the project.

Posted by Peter on September 14, 2012 at 02:30 AM CEST #

Since there are (?) a couple of issues with this simple approach, and given the fact that you are working on a cdi-integration project for vaadin 7 ... is it safe to say: this code sample looks impressive, but dont try this at home (in your production environment) for there might be some major issues?

I really dont like the cdiutils for vaadin 6, but it looks like there is no way to avoid them by now. right?

Posted by Jan on October 02, 2012 at 03:11 AM CEST #

Nice post. I am just starting with Vaadin and have some time before I need to release what I'm working on from development. I'm going to try this approach and hope the issues previously mentioned get resolved in the coming months.

Posted by mklaene on October 08, 2012 at 11:43 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License