Blog

Vaadin, CDI and Java EE compatibility

By  
Sami Ekblad
·
On Nov 27, 2014 8:01:00 AM
·
vaadin.com/javaee

Vaadin CDI support became official today. While CDI features have been in use in practice for quite some time already, this is a major step in making the use of Java EE 7 technologies easier. 

Most of you are already more than familiar with Java EE “Web Profile”. Web Profile offers a complete stack for web applications, including core web container functionality (Servlet), business logic, transactions (JTA) and persistence (JPA). But Web Profile is just a subset of Java EE. In addition to standard Java SE, Java EE provides APIs and a runtime environment for developing and running large-scale, scalable, reliable, and secure applications.

What is CDI for Vaadin?

Like the name suggests, the Vaadin CDI addon brings you one of the most interesting features of Java EE for Vaadin. ‘Contexts and Dependency Injection’ are already available in  Web Profile, and they provide a nice annotation-based mechanism to wire your Vaadin application logic without creating too many compile-time component references. Here are a few examples:

@CDIUI annotation is the way in which you let the Vaadin CDI plugin know which UIs should be accessible to the user and how they should be mapped. It accepts one optional String parameter indicating the UI path.

@Inject annotation can be used to further inject beans and serviced to your UI class. If you are using interfaces, you can use @Default and @Alternative to choose which implementation really will be used at runtime.

@CDIView is used together with a CDIViewProvider to create a navigation structure for your UI. Users can access different views directly through unique URIs, or you can change the active view programmatically with CDIViewProvider.navigateTo.

With these annotations you can build and maintain your UI structure in a very loosely-coupled way and easily access other Java EE features. But in addition to this, you most likely need control over different lifecycles and scopes. Vaadin CDI gives some new instruments for this:

@UIScoped component lifecycle is bound to a browser tab. This annotation will create a direct reference to the object rather than a proxy, but you can also use the sister annotation @NormalUIScoped that uses a proxy for the contextual instance.

@ViewScoped (and @NormalViewScoped) component lifecycle starts when the view is navigated to and ends when navigating to a different view or when the UI is closed.

@ApplicationScoped Java EE annotation allows you to effectively make a singleton (mostly, but see the CDI specification for details).

While this was just a quick introduction, there is a full tutorial on how you can build an application with CDI in the Vaadin wiki. Have a look.

Java EE compatibility matrix

Vaadin applications already run happily with Java EE Web Profile subset, but Java EE offers much more.

Java EE has its roots in 1998, when the work for an “enterprise edition” started and in 2006 (since Java EE 6) we had the concept of “profile”. A profile represents a configuration of the platform suited to a particular class of applications and that is how Java web developers came to know the “Web Profile”.

Now we are at Java EE 7 (JSR 342), an umbrella specification consisting of a total of 39 different JSRs (i.e. different APIs) and work for extending these in Java EE 8 has already started.

vaadin.com/javaee vaadin.com/javaeeWe are not going to introduce all of the JSRs here, but instead we created a “Vaadin Java EE 7 checklist” for you. Here you can find more detailed compatibility information, as well as links to examples and documentation, and get an idea of when and how to leverage Java EE APIs in your Vaadin applications.

 

 

Check out vaadin.com/javaee

 
Sami Ekblad
Sami Ekblad is one of the original members of the Vaadin team. He is now working as a framework advocate, and you can find many add-ons and code samples to help you get started with Vaadin. Follow at – @samiekblad
Other posts by Sami Ekblad