> Markdown version of [Routing](https://vaadin.com/docs/v25/flow/integrations/spring/routing). Section index: [llms.txt](https://vaadin.com/docs/v25/flow/llms.txt)

# Routing with Spring

Routing works the same way with Spring as it does in plain Vaadin applications. See [Routing and Navigation](https://vaadin.com/docs/v25/flow/routing.md) for more.

This content applies to using Vaadin with both [Spring Boot](https://vaadin.com/docs/v25/flow/integrations/spring/spring-boot.md) and [Spring MVC](https://vaadin.com/docs/v25/flow/integrations/spring/spring-mvc.md).

## <a id="defining-routes"></a>Defining Routes

To handle the default route, all you need to do is define a component with the `@Route("")` annotation.

This example is defining `RootComponent` as the default root target using the `@Route` annotation:

```java
@Route("")
public class RootComponent extends Div {
    public RootComponent(){
        setText("Default path");
    }
}
```

You can also define all other possible routes in the same way as in standard Vaadin applications. See [Defining Routes with @Route](https://vaadin.com/docs/v25/flow/routing.md) for more on using the `Router` class.

## <a id="using-dependency-injection-and-spring-autowiring"></a>Using Dependency Injection and Spring Autowiring

The only difference between using the router in a standard application and a Spring application is that, in Spring, you can use dependency injection in components annotated with `@Route`. These components are instantiated by Spring and become Spring-initialized beans. In particular, this means you can autowire other Spring-managed beans.

This example is using autowiring in a component annotated with `@Route`:

```java
@Route("")
public class RootComponent extends Div {

    public RootComponent(@Autowired DataBean dataBean) {
        setText(dataBean.getMessage());
    }
}

public interface DataBean {
    String getMessage();
}

@Component
public class DataBeanImpl implements DataBean {

    public String getMessage(){
        return "message";
    }
}
```

## <a id="routing-in-spring-boot-war-applications"></a>Routing in Spring Boot & WAR Applications

Running an application as a Spring Boot application is different from running it as a `WAR` application deployed to a web server.

In `WAR` applications, all `@Route` annotations are discovered automatically, due to the Servlet 3.0 specification. With Spring Boot applications, this is, by design, not the case. See [Spring Boot #321](https://github.com/spring-projects/spring-boot/issues/321) for more.

The [Vaadin Spring add-on](https://vaadin.com/directory/component/vaadin-spring/overview) implements scanning for router classes in Spring Boot applications. This is also true for other Vaadin types that need to be discovered and registered at start-up. However, scanning only occurs inside the Spring Boot application class package, that is, the package in which the `@SpringBootApplication` class is in. If your application contains route classes in packages that aren’t scanned by default, you have two options:

- Move them to the package (or sub-package) in which the `@SpringBootApplication` application class is in, or

- Explicitly specify the packages that should be scanned. You can specify packages to scan using the `value` parameter in the `@EnableVaadin` annotation.

> **Note:** The `@EnableVaadin` annotation is required when using [Spring MVC](https://vaadin.com/docs/v25/flow/integrations/spring/spring-mvc.md) without [Spring Boot](https://vaadin.com/docs/v25/flow/integrations/spring/spring-boot.md). When using Spring Boot, auto-configuration automatically enables Vaadin with a default location to scan for Vaadin routes. You can still use the `@EnableVaadin` annotation in a Spring Boot application specifically for the purpose of replacing the default configuration.

### <a id="route-classes-outside-the-application-package"></a>Route Classes Outside the Application Package

The following project keeps its views in a package that isn’t under the package of the `@SpringBootApplication` class:

```
src/main/java
├── com/example/application
│   └── Application.java
└── com/example/views
    └── ProfileView.java
```

To have Vaadin find `ProfileView`, list both packages in `@EnableVaadin`:

```java
@SpringBootApplication
@EnableVaadin({ "com.example.application", "com.example.views" })
public class Application implements AppShellConfigurator {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
```

The packages in `@EnableVaadin` are used for all Vaadin types that are discovered at start-up, not only for route classes. This includes the following:

- classes annotated with `@Layout` or `@RouteAlias`

- error views that implement `HasErrorParameter`

- the class that implements `AppShellConfigurator`

The example lists the application package because `Application` implements `AppShellConfigurator`.

Spring settings for the packages to scan don’t affect where Vaadin looks for these classes:

- `@ComponentScan(basePackages = …​)` and `scanBasePackages` of `@SpringBootApplication` control which classes become Spring beans. Route classes don’t need to be Spring beans, and annotating one with `@SpringComponent` doesn’t register it as a route.

- Other Spring scanning annotations, such as `@EnableJpaRepositories(basePackages = …​)` or `@EntityScan`, configure only their own feature.

The `vaadin.allowed-packages` property doesn’t add packages to scan, either. See [Configure Packages Scanning](https://vaadin.com/docs/v25/flow/integrations/spring/configuration.md#configure-packages-scanning).

`4E7C7193-B3CD-4551-8919-58297836B694`
