Introduction
Hello Ninjas,
I know that the topic Routing with Spring is itself a unique topic. If you searched on the web, you didn't find much about the Routing with Spring. So, let's discuss Routing with Spring to understand it better.
But before going into the topic, let's discuss what Routing means.
Routing in Vaadin means mapping views into specific URLs and maintaining meaningful deep linking.
Routing works similarly with Spring as in the simple Vaadin applications.
(Vaadin is a Java web app development platform that helps us build reliable web applications with great UX faster than before.)
Here we will discuss the use of Vaadin with both Spring Boot and Spring MVC.
Routing with Spring

Defining Routes
We must define a component with a @Route annotation to handle the default route.
Let's see an example where we define a RootComponent as the default root target using the @Route annotation.

Example
@Route("")
public class RootComponent extends Div {
public RootComponent(){
setText("Default path");
}
}We can also define all other possible routes in the same way as in standard Vaadin applications.
Using Dependency Injection and Spring Autowiring
In Spring, we can use dependency injection in components annotated with @Route. It is the only difference between using the router in a standard and a spring application.

Example:
Use of auto wiring 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.

As we know, running an application as a Spring Boot application and a WAR application deployed to a web server is different.
As In WAR applications, due to the Servlet 3.0 specification, all the @Route annotations are discovered automatically, and In Spring Boot applications, this is, by design, not the case.
Vaadin Spring add-on implements scanning for router classes in Spring Boot applications. It is also true for other Vaadin types that must be discovered and registered at start-ups. Although, scanning only occurs inside the Spring Boot application class package in which the @SpringBootApplication class resides. If the application contains route classes in packages that are not scanned by default, then we have two options:
-
The first is to Move them into the package (or sub-package) in which the @SpringBootApplication application class resides.
- And the second is to specify the packages that should be scanned Explicitly. We can specify packages to scan using the value parameter in the @EnableVaadin annotation.
Let's discuss frequently asked questions related to "Routing with Spring."





