Discovering modules

The webapp application should detect all the implementations of AppModule at run-time. For each implementation, it should create a new instance and call the register(ApplicationLayout) method. Doing this with Java SPI is surprisingly simple:

public class VaadinUI extends UI {

protected void init(VaadinRequest vaadinRequest) {
TabBasedApplicationLayout layout
= new TabBasedApplicationLayout("Caption");
setContent(layout);
loadModules(layout);
}

private void loadModules(
ApplicationLayout applicationLayout) {
ServiceLoader<AppModule> moduleLoader =
ServiceLoader.load(AppModule.class);
moduleLoader.forEach(
module -> module.register(applicationLayout));
}
}

The ServiceLoader class is used to discover all the classes that implement the AppModule interface. For each module, we call its register method, passing the layout of the application to give the module the chance to initialize itself and modify the layout if required.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset