NOTICE!
This post relates to an early version of Cdi Utils and many things have changed since then. For example CDI Utils no longer uses @SessionScoped but defines a custom scope for Vaadin Applications.
I’ve released a new add-on to Vaadin directory called
CDI Utils
. CDI (Contexts and Dependency Injection) is a JavaEE standard that consists of two fundamental parts: It allows you to bind bean instances to contexts with well-defined life-cycles and inject instances to beans in a type-safe way.
Using CDI in your Vaadin application increases its maintainability by making it more flexible/loosely coupled and can obsolete boilerplate code such as inter-component listeners (CDI has a built-in event bus) and Thread-local pattern (Application reference can be obtained anywhere by injecting). Vaadin fully supports CDI from version 6.7 up.
CDI Utils add-on provides some utilities for CDI/Vaadin applications:
[b]
- AbstractCdiApplication/AbstractCdiApplicationServlet (configured and ready for use)
[/b]
The AbstractCdiApplication is basically a session scoped Vaadin Application that also invalidates the session after application.close()
2. Lightweight MVP framework
2.1.Extend View interface for your view
2.2.Create view instance that extends AbstractView and implements your View extension
2.3.Extend AbstractPresenter and annotate it with @ViewInterface(YourViewInterface.class)
That’s it.
-The correct view instance is then automatically injected to your presenter (Your view control logic can then reside in the presenter)
-The view should use CDI’s built-in event bus to fire events that are observed by the presenter. The add-on provides a utility for this: fireViewEvent(ParameterDTO). The ParameterDTO can be used to transfer any data to the presenter (CDI Event observers only accept one parameter)
-Call yourViewImplementationInstance.openView() each time the view is accessed (this will eventually invoke yourPresenterInstance.viewOpened())
3.Producers for declaratively defined Vaadin components (@Preconfigured -annotation)
Inject preconfigured Vaadin Components to your views. For example:
@Preconfigured(captionKey=“btnUpdate”, implementation=NativeButton.class)
private Button button;
Notice that if you’re planning on implementing the TextBundle interface (if you intend to use the “captionKey” attribute of @Preconfigured or AbstractView.getText() that is) and using Weld version prior to 1.1.1 it will not work. Library-to-application visibility has been fixed in Weld 1.1.1,
Also, due to limitations of default CDI scope types, only one Vaadin Application is allowed/session. This is a known issue and fixing it would require implementing a custom CDI scope for Vaadin applications. I’ll look into this later but currently all components and application can be bound to session scope as the session will be invalidated by AbstractCdiApplication after it’s been closed.
Quick start:
- Add empty beans.xml -file (CDI marker file) to your project under WEB-INF dir
- Add cdiutils*.jar to your project under WEB-INF/lib
- Create your Application class by extending AbstractCdiApplication
- Extend AbstractCdiApplicationServlet and annotate it with @WebServlet(urlPatterns = “/*”)
- Deploy to JavaEE/Web profile -compatible container (CDI apps can also be run on servlet containers etc. but some further configuration is required)