JavaData & JavaGetting Started with Spring and Vaadin

Getting Started with Spring and Vaadin content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Vaadin, down the years, has matured; so has Spring. They can be a killer combination when it comes to developing Web-based applications. Vaadin can be used do the presentation work whereas Spring does all the heavy lifting of delivering business services. This article provides an overview of integrating these two frameworks and shows a simple example of how they can be used in developing Web-based applications.


There are numerous presentation layer frameworks available for developing Java-based Web applications. Each of them has its way in delivering the developer’s specific needs. Vaadin distinctively is one among them. An interesting aspect of Vaadin as a presentation layer framework is that we can use plain Java code to design a functional Web interface, much like the Swing framework is used to design a desktop application interface. Although not limited to Java code-based approach only, Vaadin also provides an alternate option for HTML-based design. These clearly distinguish Vaadin from other JavaScript-based frameworks.


Spring is an open source framework to develop Java enterprise applications. In fact, it provides almost everything needed to create any Java-based applications, including support for Groovy and Kotlin as alternative languages on the JVM. The Spring framework is divided into modules; as a result, they can be nitpicked and used according to your need. The heart of the modules is the core container with a configuration model and dependency injection mechanism. Apart from this, there id foundational support for different application architectures, such as Web, messaging, transaction, and persistence. It also has the support of the Servlet-based Spring MVC Web framework and Spring Web Flux reactive Web framework.

Historically, Spring began its journey in 2003 as a simpler alternative to the J2EE specification, and today has emerged as a fine complement to Java EE technology by selectively integrating distinct specifications from the Java EE spectrum, such as Concurrency utilities (JSR 236), Bean Validation (JSR 303), Servlet API (JSR 340), WebSocket API (JSR 356), JSON Binding API (JSR 367), JMS (JSR 914), JPA (JSR 338), and so forth, to name a few.

The evolution with Spring 5 went further into much more than just a framework and shifted the paradigm of Java programming once and for all with innovative projects such as Spring Boot, Spring Batch, Spring Integration, Spring Data, Spring Cloud, and so on.

In our following example, we’ll use Spring Boot to transitively import all the dependencies required for the application. In association with Vaadin, Spring shall do the heavy lifting of providing background business services using JPA technology and Vaadin shall provide the view for data presentation in the browser.


Vaadin is an open source Web application development platform that provides a set of tools and starters, apart from its flagship product called Vaadin Flow (previously called Vaadin framework). In short, it started as a framework that enabled implementation of an HTML Web user interface using Java programming. This is what is called Vaadin Flow now. Later on, it started proving high-level component APIs that do more than just presentation work. According to official documentation, Vaadin provides:

  • Automatic support for client-server communication using WebSocket
  • Support for building Web views using pure Java code or HTML
  • Data binding support
  • Route with nested route and parameter support
  • Other JVM language support such as Kotlin and Scala
  • Built-in Spring support

Integrating Vaadin and Spring

Spring, as a framework, is an open design for integrating various other technologies. As a result, it is no surprise that Vaadin would find its niche in Spring, almost effortlessly. It would be an exaggeration to say that Vaadin is an exceptional web UI design framework, but it serves its purpose. It is distinct because it allows a Java code-based design approach. A Java developer who mainly focuses on the business service development rarely finds interest in UI design. Vaadin is a good choice for them. Vaadin can be used to develop a highly responsive Web interface, yet, perhaps the glorious part of it is that it allows a Java developer a little idea of how JavaScript is able to develop a functional, decent Web interface using Java code only. That singularly deserves kudos for the Vaadin developers.

As stated, that Spring is an open-ended architecture, integrating them is like walking in the park, thanks to Spring Boot. Here, we’ll try a very simple example; nothing fancy, of course, with Spring JPA functionality. The code is kept simple and elaborate to make it as lucid as possible.

A Quick Example

<?xml version="1.0" encoding="UTF-8"?>
      Demo project for Spring Boot
         <!-- look up parent from repository -->

Listing 1: pom.xml

server.port = 8080
# ===============================
# ===============================
spring.datasource.url = jdbc:mysql://localhost:3306/testdb
spring.datasource.username = root
spring.datasource.password = secret
pring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
# ===============================
# =============================== = true
spring.jpa.hibernate.ddl-auto = create-drop
#spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy =
   org.hibernate.cfg.ImprovedNamingStrategy =

Listing 2:

package com.mano.springvaadinapp1.model;
import javax.persistence.*;
public class Employee {
   privateLong id;
   private String name;
   // Contact
   private String phone;
   private String email;
   public Employee() {
   public Employee(String name, String phone, String email) { = name; = phone; = email;
   public Long getId() {
      return id;
   public void setId(Long id) { = id;
   public String getName() {
      return name;
   public void setName(String name) { = name;
   public String getPhone() {
      return phone;
   public void setPhone(String phone) { = phone;
   public String getEmail() {
      return email;
   public void setEmail(String email) { = email;
   public String toString() {
      return "Employee{" +
         "id=" + id +
         ", name='" + name + ''' +
         ", phone='" + phone + ''' +
         ", email='" + email + ''' +

Listing 3:

package com.mano.springvaadinapp1.repository;
import com.mano.springvaadinapp1.model.Employee;
public interface EmployeeRepository extends
      JpaRepository<Employee, Long> {
   Employee findByName(String name);

Listing 4:

package com.mano.springvaadinapp1.ui;
import com.mano.springvaadinapp1.model.Employee;
import com.mano.springvaadinapp1.repository.EmployeeRepository;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.formlayout.FormLayout;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.listbox.ListBox;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.router.Route;
import org.springframework.beans.factory.annotation.Autowired;
public class TestUI extends HorizontalLayout{
   private TextField txtname, txtphone,
   private Button btnAddNew;
   private ListBox<Employee> listBox =
      new ListBox<>();
   private EmployeeRepository repo;
   public TestUI(EmployeeRepository repo){
      this.repo = repo;
      listBox.setRenderer(new ComponentRenderer<>(emp -> {
         Label name = new Label("Name: " + emp.getName());
         Label phone = new Label("Phone: " + emp.getPhone());
         Label email = new Label("Email: " + emp.getEmail());
         NativeButton button = new NativeButton("Delete",
               event -> {
         Div labels = new Div(name, phone,email);
         Div layout = new Div(labels, button);
         labels.getStyle().set("display", "flex")
            .set("flexDirection", "column")
            .set("marginRight", "10px");
         layout.getStyle().set("display", "flex")
            .set("alignItems", "center");
         return layout;
      listBox.setItemEnabledProvider(employee -> {
         employee = repo.findByName(employee.getName());
         if (employee!=null)
            return true;
            return false;
      listBox.addValueChangeListener(event ->
      add(listBox, createForm());
   private FormLayout createForm(){
      FormLayout f = new FormLayout();
      txtname = new TextField("Name");
      txtphone = new TextField("Phone");
      txtemail = new TextField("Email");
      btnAddNew = new Button("Add New",
      return f;
   private void save(){
      if (txtname.isEmpty() || txtphone.isEmpty()
            || txtemail.isEmpty()) {"Input data cannot be empty");
      Employee emp = repo.findByName(txtname
      if (emp==null) { Employee(txtname.getValue(),
            txtphone.getValue(), txtemail.getValue()));
      }else{"Cannot save. Same name
            exists. Try different name.");
   public void clearFormData(){
   public void populateForm(Employee emp){

Listing 5:

package com.mano.springvaadinapp1;
import com.mano.springvaadinapp1.model.Employee;
import com.mano.springvaadinapp1.repository
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure
import org.springframework.context.annotation.Bean;
public class Application {
   private static final Logger log =
   public static void main(String[] args) {;

   publicCommandLineRunner loadData(EmployeeRepository
         repository) {
      return (args) -> { Employee("Sachin Tendulkar",
            "987654321","")); Employee("Kapil Dev",
            "34543543","")); Employee("Sunil Gavaskar",
            "565534321","")); Employee("Ravi Sastri",
            "3445543521","")); Employee("Navjit Sidhu",
         // Fetch all employees"Employees found with findAll():");"-------------------------------");
         for (Employee e : repository.findAll()) {
         Employee employee= repository.findById(1L).get();"Employee found with findOne(1L):");"--------------------------------");;"");

Listing 6:


Output of the running program
Figure 1: Output of the running program


It is fun to integrate Spring and Vaadin; they work together seamlessly. The example given above is to give an idea how to integrate both into the framework. Note that the application above is not thoroughly tested; hence, it may have some runtime issues, my bad! I did it in a hurry. Feel free to take it from there and enhance it. Happy coding 🙂


Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories