Table of contents
1.
Introduction
2.
Routing with Spring
2.1.
Defining Routes
2.1.1.
Example
2.2.
Using Dependency Injection and Spring Autowiring
2.3.
Example: 
2.4.
Routing in Spring Boot and WAR applications.
3.
Frequently Asked Questions
3.1.
What do you understand by Spring Boot?
3.2.
What is @configuration in spring boot?
3.3.
What is Dependency Injection?
3.4.
What are the limitations of autowiring?
4.
Conclusion
Last Updated: Aug 13, 2025
Medium

Routing with Spring

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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

Routing with Spring Image

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.

Defining Routes Image

Example

@Route("")
public class RootComponent extends Div {
    public RootComponent(){
        setText("Default path");
    }
}
You can also try this code with Online Java Compiler
Run Code

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 Image

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";
    }
}
You can also try this code with Online Java Compiler
Run Code

Routing in Spring Boot and WAR applications.

WAR Applications Image

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."

Frequently Asked Questions

What do you understand by Spring Boot?

Spring Boot is a java-based framework that supports Rapid Application Development and gives a platform for developing stand-alone and production-ready spring applications.

What is @configuration in spring boot?

Spring @Configuration annotation is part of the spring core framework, indicating that the class has @Bean definition methods. 

What is Dependency Injection?

Dependency Injection means that we don't have to create our objects, but we have to describe how they should be created.

What are the limitations of autowiring?

Overriding possibility and Data type restrictions are some limitations of autowiring.

Conclusion

In this article, We have discussed Routing with Spring with examples. We also covered Routing in Spring Boot and WAR applications, followed by some frequently asked questions on the Routing with Spring.

Recommended Readings:

After reading about the Routing with Spring, are you not feeling excited to read/explore more articles on Data Structures and Algorithms? Don't worry; Coding Ninjas has you covered. You can also consider our Spring Boot Course to give your career an edge over others.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Conclusion Image

Live masterclass