Documentation

Documentation versions (currently viewingVaadin 23)

You are viewing documentation for Vaadin 23. View latest documentation

Service Init Listener

Using VaadinServiceInitListener to configure RequestHandler, IndexHtmlRequestListener and DependencyFiler objects, and to dynamically register routes during start-up.

VaadinServiceInitListener can be used to configure RequestHandler, IndexHtmlRequestListener and DependencyFilter objects. You can also use it to dynamically register routes during application startup, and to register session and UI listeners.

The listener gets a ServiceInitEvent, which is sent when a Vaadin service is initialized.

public class ApplicationServiceInitListener
        implements VaadinServiceInitListener {

    @Override
    public void serviceInit(ServiceInitEvent event) {
        event.addIndexHtmlRequestListener(response -> {
            // IndexHtmlRequestListener to change the bootstrap page
        });

        event.addDependencyFilter((dependencies, filterContext) -> {
            // DependencyFilter to add/remove/change dependencies sent to
            // the client
            return dependencies;
        });

        event.addRequestHandler((session, request, response) -> {
            // RequestHandler to change how responses are handled
            return false;
        });
    }

}

In a Spring Boot project, it’s sufficient to register this listener by adding the @Component annotation on the class. In plain Java projects, the listener should be registered as a provider via the Java Service Provider Interface (SPI) loading facility. To do this, you should create the META-INF/services resource directory and a provider configuration file with the name com.vaadin.flow.server.VaadinServiceInitListener. This is a text file and should contain the fully qualified name of the ApplicationServiceInitListener class on its own line. It allows the application to discover the ApplicationServiceInitListener class, instantiate it and register it as a service init listener for the application.

The location of the configuration file

The content of the file should look like this:

com.mycompany.ApplicationServiceInitListener

EA8B92C9-D967-4C55-B760-FEBEEA964D72