> Markdown version of [Working with Spring MVC](https://vaadin.com/docs/v14/flow/integrations/spring/tutorial-spring-basic-mvc). Section index: [llms.txt](https://vaadin.com/docs/v14/flow/llms.txt)

# Using Vaadin with Spring MVC

In this section we cover how to use Vaadin with [Spring MVC](https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html). Spring MVC is the original Spring web framework built on the Servlet API.

> **Note:** See [Using Vaadin with Spring Boot](https://vaadin.com/docs/v14/flow/integrations/spring/tutorial-spring-basic.md) to use Vaadin with [Spring Boot](https://spring.io/projects/spring-boot).

## <a id="registering-the-vaadin-servlet"></a>Registering the Vaadin Servlet

To use Vaadin in your Spring web application you need to register the Vaadin `SpringServlet` as a dispatcher servlet.

**Example**: Registering the `SpringServlet` as a dispatcher servlet.

```java
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, true));
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");
    }

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

## <a id="registering-vaadin-scopes"></a>Registering Vaadin Scopes

To use [Vaadin Spring scopes](https://vaadin.com/docs/v14/flow/integrations/spring/tutorial-spring-scopes.md) you need to register the `VaadinScopesConfig` configuration class. As an alternative, you can add the `@EnableVaadin` annotation to your configuration class to import `VaadinScopesConfig`.

The [Vaadin Spring add-on](https://vaadin.com/directory/component/vaadin-spring/overview) provides the `VaadinMVCWebAppInitializer` class that is an abstract subclass of the `WebApplicationInitializer` class. You can extend this class and provide your configuration classes by implementing the `getConfigurationClasses()` method.

**Example**: Extending `VaadinMVCWebAppInitializer` and implementing the `getConfigurationClasses()` method.

```java
public class SampleWebAppInitializer
        extends VaadinMVCWebAppInitializer {

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

```java
@Configuration
@ComponentScan
public class SampleConfiguration {
}
```

- This registers `VaadinScopesConfig` and `VaadinServletConfiguration` automatically.

## <a id="handling-urls"></a>Handling URLs

To handle URLs, you need at least one Vaadin component, annotated with `@Route`. See [Handling URLs](https://vaadin.com/docs/v14/flow/integrations/spring/tutorial-spring-basic.md#handling-urls) for an `@Route` annotation example.

## <a id="declaring-dependencies"></a>Declaring Dependencies

To use your Spring web application, you need to declare dependencies in your `pom.xml` file to `vaadin-bom` and `spring-web` as follows:

```xml
<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>
```

`5DBA5D12-7B4C-4ACA-998A-C1D15F12DD8C`
