Contexts and Dependency Injection for Vaadin
This add-on permits the use of primary CDI (JSR-299) features such as dependency injection, CDI events, etc. It also defines a new CDI scope for UIs and automatically configures a servlet for CDI if no Vaadin servlets are defined.
|Vaadin CDI||Vaadin version|
If you do not customize Vaadin Servlet in your web.xml, a CDI enabled Vaadin servlet is deployed automatically.
As UI class CdiUI is configured. If you need a custom UI class, you have to extend this class. It is not a CDI contextual instance but injects work.
Otherwise, you can customize CdiVaadinServlet just like VaadinServlet.
Component instantiation and CDI
Vaadin triggered instantiation happens in a CDI aware Vaadin Instantiator implementation. Components created by this API:
- @Route, RouteLayout, HasErrorParameter components
- component fields injected by @Id to polymer templates
By default, instantiator looks up the CDI bean by type ( component class )
and gets a contextual reference from BeanManager. All the CDI features are usable like observer, interceptor, decorator.
When type is not found as a CDI bean ( for example ambiguous, or does not have a no-arg public constructor ), instantiation falls back to the default Vaadin behavior. On success, dependency injection is performed. Injects work, but other CDI features not. It is not a contextual instance.
In addition to standard CDI contexts, the CDI add-on introduces some new contexts. Read more about the supported scopes in Vaadin Docs
Some Vaadin service interfaces can be implemented as a CDI bean.
Beans have to be qualified by @VaadinServiceEnabled to be picked up automatically.
Example code is available in Vaadin Docs
Following events fired as a CDI event:
See example code in Vaadin Docs
Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.
1.0.5 has following fixes:
- Improvements in the closed
@PreDestroyis now called properly
VaadinServletdetection with Liberty, JAX-WS and JAX-RS
In this version there is a possible issue still in
UI cleanup with WELD.
- Apache License 2.0
- Vaadin 7.3+
- Vaadin 7.0+ in 1.0.0.alpha1
- Vaadin 7.2+ in 1.0.0.alpha2
- Vaadin 8.0+ in 2.0.0.beta1
- Vaadin 8.1+ in 3.0.0.alpha1
- Vaadin 7.7+ in 1.0.4
- Vaadin 8.2+ in 3.0.0
- Vaadin 10+ in 10.0.0
- Vaadin 14+ in 11.0.0
- Browser Independent