New modules have to implement the AppModule interface and follow the SPI requirements for packaging by adding a new file with the name packt.vaadin.datacentric.chapter02.api.AppModule into the META-INF/services directory. This file must contain the name of the fully qualified name of the AppModule implementation.
Let's say you want to develop a module that adds an option to the main menu that shows a notification when clicked. This can be easily implemented as follows:
package com.example;
...
public class ExampleModule implements AppModule {
@Override
public void register(ApplicationLayout layout) {
ApplicationLayout.MenuOption menuOption
= new ApplicationLayout.MenuOption("Example module");
layout.addMenuOption(menuOption, this::optionClicked);
}
private void optionClicked(
ApplicationLayout.MenuOption menuOption) {
Notification.show("It works!",
Notification.Type.TRAY_NOTIFICATION);
}
}
This class can be located in a separate Maven project and should include the api dependency.
To make the module discoverable by the webapp application, you must add a file with the name packt.vaadin.datacentric.chapter02.api.AppModule in the main/resources/META-INF/services directory of the new module. The file must contain the fully-qualified name of the AppModule implementation as follows:
packt.vaadin.datacentric.chapter02.example.module.ExampleModule
Once packaged, you can deploy the JAR file independently and the webapp application should automatically discover and register the module.
The following is a screenshot of the application, showing the example module in action: