CDI and Vaadin Application Setup


I just thought a little bit about the problem of using CDI and Vaadin. I looked at the recommended Vaadin way to setup an application.
Basically you configure your application class, which is then returned by the servlet as simple as by calling newInstance().

So Vaadin seems to be clever enough to call getNewApplication() only when needed, i.e. once per HttpSession.

If you want to use injection properly and thread safe, why not just inject your application like

Instance<MyApplication> appl;

Here is the app itself:

public class MyApplication extends Application {

and on getNewApplication() just do…

return appl.get()

In my understanding that should do exactly the same as the default Vaadin ApplicationServlet but you can use injection on the application for instance to inject your services and EJB.

This seems to eliminate the need of invalidating the HttpSession as well, as the Application is not SessionScoped.

I wonder if this approach would work fine and why the propoed approaches are that complicated. The only issue I see atm is that you can’t inject the application at several places, but I don’t see the actual need for this.

OK, the issue here is that the application instances will never be removed from the heap. I guess that has to do with some internal management stuff for CDI.
The doc says that dependant scope will depend on the scope of the enclosing instance, so as this is a servlet I guess they would only be removed when the servlet is removed. Didn’t analyze it in detail. But seems not to work

I tried

@Inject @New

but this leads to some serious exceptions :wink: I guess this is some bug, as the documentation actually uses a similar example.