Use Flow with Spring MVC

In the previous section it has been shown the easiest way to use vaadin-spring add-on with Spring Boot. If you are not familiar with Spring Boot magic or it’s just too magic for you and you want to write pure Spring Web application then you can follow this section.

You may create your own Spring Web application from scratch. To be able to use Flow in your Spring Web application you should register Vaadin servlet as a dispatcher servlet.

public abstract class ExampleWebAppInitializer
        implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        registerConfiguration(context);
        servletContext.addListener(new ContextLoaderListener(context));

        ServletRegistration.Dynamic registration = servletContext
                .addServlet("dispatcher", new SpringServlet(context));
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");
    }

    private void registerConfiguration(
            AnnotationConfigWebApplicationContext context) {
        // register your configuration classes here
    }
}

You will need some Flow component annotated with @Route and you should register the VaadinScopesConfig configuration class to be able to use Spring Vaadin scopes (see the Flow application code examples in Use Flow with Spring).

Note
Alternatively, you may add the @EnableVaadin annotation to your configuration class to import VaadinScopesConfig.

The Spring add-on provides an abstract subclass of WebApplicationInitializer class which you may just extend and provide your configuration classes via implementing the getConfigurationClasses() method:

public class SampleWebAppInitializer extends VaadinMVCWebAppInitializer {

    @Override
    protected Collection<Class<?>> getConfigurationClasses() {
        return Collections.singletonList(SampleConfiguration.class);
    }
}

@Configuration
@ComponentScan
public class SampleConfiguration {
}
Note
VaadinScopesConfig and VaadinServletConfiguration configurations will be registered automatically for you in this case.

To be able to use Spring Web application you should declare dependencies in your pom.xml file to vaadin-bom and spring-web:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <version>${vaadin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-spring</artifactId>
    </dependency>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.0.2.RELEASE</version>
    </dependency>