8 Replies Latest reply: Jun 7, 2011 8:26 AM by Laird Nelson RSS

CDI + Vaadin

Aaron Siri Newbie

I'm playing with Vaadin and would like to use CDI with it.  However, I've run into a problem which makes me think I'm not understanding something fully.


In Vaadin (and most similar frameworks) you define an application class that, when provided by a Servlet, provides the starting point for the application.


When the Vaadin application class is scoped (i.e. @SessionScoped) @Inject works and I can use CDI to @Inject beans into it.  However, none of the other UI classes that I write (panels, etc.) seem to work with @Inject - the @Injected items are just null (I'm testing by using the security beans, Identity and Credentials.)  Is there a way to get these other UI classes recognized so that I can @Inject into them?


Thanks for any help,


-Aaron

  • 1. Re: CDI + Vaadin
    Nicklas Karlsson Master

    Have a look at this article and check with the Vaadin forums.

  • 2. Re: CDI + Vaadin
    Aaron Siri Newbie

    I looked at those and used them to get started but they only show how to get the Application class annotated.  They don't show annotating any other UI classes.  The forums also mention has this is currently an issue with CDI and suggest accessing all context beans through the application class.  I'm wondering why it has to be that way and you can't just @Inject into UI classes directly.


    -Aaron

  • 3. Re: CDI + Vaadin
    Nicklas Karlsson Master

    Ask them to come over here and talk about it if they think there is some issue with CDI

  • 4. Re: CDI + Vaadin
    Cloves Almeida Newbie

    IIRC, if you instantiate a bean using new keyword, it simply won't be managed, thus can't use @Inject, be intercepted, etc. When building your layout you should inject your component into the Application and then it should be managed.

  • 5. Re: CDI + Vaadin
    Ricardo Memoria Newbie

    Hi...
    ... I saw this post and decided to answer my experience.


    Since Vaadin is processed inside a Servlet, if you follow SEAM instructions to context management for custom servlets, everything will work fine:


    1. Declare the seam filter in the web.xml




        <filter>
            <filter-name>Seam Filter</filter-name>
            <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>Seam Filter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>





    2. Declare your Vaadin application Servlet (as described in Vaadin book)


    3. in components.xml, enable the context filter




    <web:context-filter url-pattern="/vaadin-app-url/*"/>
    




    4. Inside Vaadin servlet, use SEAM API, like Component and Contexts classes


    For me it's working perfectly.


    Cheers,
    Ricardo

  • 6. Re: CDI + Vaadin
    Hendy Irawan Newbie

    For CDI, you can also use the method described in the Vaadin wiki :


    http://vaadin.com/wiki/-/wiki/Main/Creating%20JEE6%20Vaadin%20Applications


    (Option 2: Use CDI)


    That methods subclasses the Vaadin servlet so it's initialized by CDI using @SessionScoped, as well as getting your Vaadin application via @Inject)


    I'm not sure which one is better architecturally, but that method has no dependency on Seam or any other third-party library at all.

  • 7. Re: CDI + Vaadin
    I need a real name Newbie

    Hi Ricardo, your solution is working for me too! Thx for the trick..


    Nevertheless, something is still missing on my side. How did you manage transactions ?


    Thx in advance.


    S.

  • 8. Re: CDI + Vaadin
    Laird Nelson Newbie

    Wouldn't Vaadin be required to have a META-INF/beans.xml in its main jar file if its artifacts were to be considered for injection?  Just testing my assumptions.  I am pretty sure their jar file has no such file.  Apologies if this is all painfully obvious or stupid.


    Best,

    Laird