Vaadin Service Interfaces as CDI Beans

Some Vaadin service interfaces can be implemented as CDI beans. If you do this, the service interface becomes a managed bean with CDI features, and there is no need to register the implementation in Vaadin.

The Vaadin CDI add-on references the following interfaces:

  • I18NProvider.

  • Instantiator.

  • SystemMessagesProvider.

  • ErrorHandler.

To ensure that the beans are recognized, they should be qualified by the @VaadinServiceEnabled annotation.

Example: Using the @VaadinServiceEnabled annotation to qualify TestSystemMessagesProvider.

Java

  1. @VaadinServiceEnabled
  2. @VaadinServiceScoped
  3. public class TestSystemMessagesProvider
  4. implements SystemMessagesProvider {
  5. @Override
  6. public SystemMessages getSystemMessages(
  7. SystemMessagesInfo systemMessagesInfo) {
  8. CustomizedSystemMessages messages =
  9. new CustomizedSystemMessages();
  10. messages.setInternalErrorMessage(
  11. "Sorry, something went wrong :(");
  12. return messages;
  13. }
  14. }
  • The purpose of the @VaadinServiceScoped context is to define a context with the lifespan of the Vaadin service. It is not mandatory for this kind of bean, but is recommended because other Vaadin contexts can be problematic. For example there is no guarantee that an active Vaadin session or UI context exists when the add-on looks up any of these beans. It is safe to use standard CDI @Dependent and @ApplicationScoped contexts.