Docs

Documentation versions (currently viewingVaadin 14)

You are viewing documentation for an older Vaadin version. View latest documentation

Using Routing with Spring

Routing works the same way with Spring as it does in plain Vaadin applications. See Routing and Navigation for more.

This content applies to using Vaadin with both Spring Boot and Spring MVC.

Defining Routes

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

Example: Defining RootComponent as the default root target using the @Route annotation.

@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 for more about using the Router class.

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.

Example: Using autowiring in a component annotated with @Route.

@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";
    }
}

Routing in Spring Boot and WAR Applications

There is a difference between running an application as a Spring Boot application and 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 for more.

The Vaadin Spring add-on 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 startup. However, scanning only occurs inside the Spring Boot application class package, that is the package in which the @SpringBootApplication class resides. If your application contains route classes in packages that are not scanned by default, you have two options: move them to the package (or subpackage) in which the @SpringBootApplication application class resides, or explicitly specify the packages that should be scanned. You can specify packages to scan using the value parameter in the @EnableVaadin annotation.

Note
You do not need to use the @EnableVaadin annotation at all with Spring Boot. This annotation is intended to be used with Spring MVC to enable the Vaadin configuration. In Spring Boot, auto-configuration is available which makes it work out of the box. The only reason to use @EnableVaadin is to specify the packages to scan with Spring MVC.

D3733BEB-5A5A-4505-8AB2-3BEFC6FAB331