Vaadin Spring Add-on

Vaadin Spring and Vaadin Spring Boot add-ons make it easier to use Spring in Vaadin applications. Vaadin Spring enables Spring dependency injection with custom UI and view providers, and provides three custom scopes: UIScope, ViewScope, and VaadinSessionScope.

API documentation for add-ons is available at:

To learn more about Vaadin Spring, the Vaadin Spring Tutorial gives a hands-on introduction. The source code of the Spring Tutorial demo is available for browsing or cloning at github.com/Vaadin/spring-tutorial.

Spring Overview

Spring Framework is a Java application framework that provides many useful services for building applications, such as authentication, authorization, data access, messaging, testing, and so forth. In the Spring core, one of the central features is dependency injection, which accomplishes inversion of control for dependency management in managed beans. Other Spring features rely on it extensively. As such, Spring offers capabilities similar to CDI, but with integration with other Spring services. Spring is well-suited for applications where Vaadin provides the UI layer and Spring is used for other aspects of the application logic.

Spring Boot is a Spring application development tool that allows creating Spring applications easily. Vaadin Spring Boot builds on Spring Boot to allow creating Vaadin Spring applications easily. It starts up a servlet, handles the configuration of the application context, registers a UI provider, and so forth.

Regarding general Spring topics, we recommend the following Spring documentation:

Dependency Injection

Dependency injection is a way to pass dependencies (service objects) to dependent objects (clients) by injecting them in member variables or initializer parameters, instead of managing the lifecycle in the clients or passing them explicitly as parameters. In Spring, injection of a service object to a client is configured with the @Autowired annotation.

For a very typical example in a web application, you could have a user data object associated with a user session:

Java

  1. @SpringComponent
  2. @VaadinSessionScope
  3. public class User implements Serializable {
  4. private String name;
  5. public void setName(String name) {this.name = name;}
  6. public String getName() {return name;}
  7. }

The @SpringComponent annotation allows for automatic detection of managed beans by Spring. (The annotation is exactly the same as the regular Spring @Component, but has been given an alias, because Vaadin has a Component interface, which can cause trouble.)

Now, if we have a UI view that depends on user data, we could inject the data in the client as follows:

Java

  1. public class MainView extends CustomComponent implements View {
  2. User user;
  3. Label greeting = new Label();
  4. @Autowired
  5. public MainView(User user) {
  6. this.user = user;
  7. ...
  8. }
  9. ...
  10. @Override
  11. public void enter(ViewChangeEvent event) {
  12. // Then you can use the injected data
  13. // for some purpose
  14. greeting.setValue("Hello, " + user.getName());
  15. }
  16. }

Here, we injected the user object in the constructor. The user object would be created automatically when the view is created, with all such references referring to the same shared instance in the scope of the Vaadin user session.

Contexts and Scopes

Contexts in Spring are services that manage the lifecycle of objects and handle their injection. Generally speaking, a context is a situation in which an instance is used with a unique identity. Such objects are essentially “singletons” in the context. While conventional singletons are application-wide, objects managed by a Spring container can be “singletons” in a more narrow scope: a user session, a particular UI instance associated with the session, a view within the UI, or even just a single request. Such a context defines the lifecycle of the object: its creation, use, and finally its destruction.

Earlier, we introduced a user class defined as session-scoped:

Java

  1. @SpringComponent
  2. @VaadinSessionScope
  3. public class User {

Now, when you need to refer to the user, you can use Spring injection to inject the session-scoped “singleton” to a member variable or a constructor parameter; the former in the following:

Java

  1. public class MainView extends CustomComponent implements View {
  2. @Autowired
  3. User user;
  4. Label greeting = new Label();
  5. ...
  6. @Override
  7. public void enter(ViewChangeEvent event) {
  8. greeting.setValue("Hello, " + user.getName());
  9. }
  10. }

Quick Start with Vaadin Spring Boot

The Vaadin Spring Boot is an add-on that allows for easily creating a project that uses Vaadin Spring. It is meant to be used together with Spring Boot, which enables general Spring functionalities in a web application.

You can use the Spring Initializr at start.spring.io website to generate a project, which you can then download as a package and import in your IDE. Pick Vaadin as a dependency on the site to include all the required Vaadin Spring dependencies and auto-configuration.The generated project is a Spring application stub; you need to add at least a UI class to the generated project.

See the Vaadin Spring Tutorial for detailed instructions for using Spring Boot.

Installing Vaadin Spring Add-on

To install the Vaadin Spring and/or Vaadin Spring Boot add-ons, either define them as an Ivy or Maven dependency or download from the Vaadin Directory add-on page at vaadin.com/directory#addon/vaadin-spring or vaadin.com/directory#addon/vaadin-spring-boot. See “Using Vaadin Add-ons” for general instructions for installing and using Vaadin add-ons.

The Ivy dependency is as follows:

  1. <dependency org="com.vaadin" name="vaadin-spring"
  2. rev="latest.release"/>

The Maven dependency is as follows:

  1. <dependency>
  2. <groupId>com.vaadin</groupId>
  3. <artifactId>vaadin-spring</artifactId>
  4. <version>LATEST</version>
  5. </dependency>

For Vaadin Spring Boot, depending on vaadin-spring-boot-starter will include all the required Vaadin dependencies.

Preparing Application for Spring

A Vaadin application that uses Spring must have a file named applicationContext.xml in the WEB-INF directory. Using Spring Initializr automatically generates a suitable file, but if you configure Vaadin Spring manually, you can follow the model below.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  10. <!-- Configuration object -->
  11. <bean class="com.example.myapp.MySpringUI.MyConfiguration" />
  12. <!-- Location for automatically scanned beans -->
  13. <context:component-scan
  14. base-package="com.example.myapp.domain" />
  15. </beans>

The application should not have a servlet extending VaadinServlet, as Vaadin servlet has its own SpringVaadinServlet that is deployed automatically. If you need multiple servlets or need to customize the Vaadin Spring servlet, see Deploying Spring UIs and Servlets.

You can configure managed beans explicitly in the file, or configure them to be scanned using the annotations, which is the preferred way described in this section.

Injecting a UI with @SpringUI

Vaadin Spring offers an easier way to instantiate UIs and to define the URL mapping for them than the usual ways described in “Deploying an Application”. It is also needed for enabling Spring features in the UI. To define a UI class that should be instantiated for a given URL, you simply need to annotate the class with @SpringUI. It takes an optional path as parameter.

Java

  1. @SpringUI(path="/myniceui")
  2. @Theme("valo")
  3. public class MyNiceUI extends UI {
  4. ...

The path in the URL for accessing the UI consists of the context path of the application and the UI path, for example, /myapp/myniceui. Giving empty UI path maps the UI to the root of the application context, for example, /myapp.

Java

  1. @SpringUI

See Deploying Spring UIs and Servlets for how to handle servlet URL mapping of Spring UIs when working with multiple servlets in the same web application.

Scopes

As in programming languages, where a variable name refers to a unique object within the scope of the variable, an object has unique identity within a scope in Spring. However, instead of identifying the objects by variable names, they are identified by their type (object class) and any qualifiers they may have.

The scope of an object can be defined with an annotation to the class as follows:

Java

  1. @VaadinSessionScope
  2. public class User {
  3. ...

Defining a scope in Spring is normally done with the @Scope annotation. For example, @Scope(“prototype”) creates a new instance every time one is requested/auto-wired. Such standard scopes can be used with some limitations. For example, Spring session and request scopes do not work in background threads and with certain push transport modes.

Vaadin Spring provides three scopes useful in Vaadin applications: a session scope, a UI scope, a view scope, all defined in the com.vaadin.spring.annotation package.

@VaadinSessionScope

The session scope is the broadest of the custom scopes defined in Vaadin Spring. Objects in the Vaadin session scope are unique in a user session, and shared between all UIs open in the session. This is the most basic scope in Vaadin applications, useful for accessing data for the user associated with the session. It is also useful when updating UIs from a background thread, as in those cases the UI access is locked on the session and also data should be in that scope.

@UIScope

UI-scoped beans are uniquely identified within a UI instance, that is, a browser window or tab. The lifecycle of UI-scoped beans is bound between the initialization and closing of a UI. Whenever you inject a bean, as long as you are within the same UI, you will get the same instance.

Annotating a Spring view (annotated with @SpringView as described later) also as @UIScoped makes the view retain the same instance when the user navigates away and back to the view.

@ViewScope

The annotation enables the view scope in a bean. The lifecycle of such a bean starts when the user navigates to a view referring to the object and ends when the user navigates out of the view (or when the UI is closed or expires).

Views themselves are by default view-scoped, so a new instance is created every time the user navigates to the view.

View Navigation

Vaadin Spring extends the navigation framework in Vaadin, described in “Navigating in an Application”. It manages Spring views with a special view provider and enables view scoping. Furthermore, Vaadin Spring provides a customized navigator class SpringNavigator that supports the scope functionality.

Preparing the UI

You can define navigation for any single-component container, component container or bean implementing ViewDisplay, as described in “Setting Up for Navigation”, but typically you set up navigation for the entire UI content. The easiest way to set up navigation is to use the annotation @SpringViewDisplay on the UI (in which case the whole contents of the UI are replaced on navigation) or on any UI scoped bean implementing one of the above mentioned interfaces.

Java

  1. @SpringUI(path="/myspringui")
  2. @SpringViewDisplay
  3. public class MySpringUI extends UI {
  4. @Override
  5. protected void init(VaadinRequest request) {
  6. }
  7. }

If not using Spring Boot, auto-configuration of navigation can be enabled with the annotation @EnableVaadinNavigation on a configuration class.

The View

A view managed by Vaadin Spring only needs to have the @SpringView annotation, which registers the view in the SpringViewProvider. The annotation is also necessary to enable Spring features in the view, such as injection.

Java

  1. @SpringView(name=MainView.NAME)
  2. public class MainView extends CustomComponent implements View {
  3. public static final String NAME = "main";
  4. ...

The annotation can have the following optional paramers:

name (optional)

Specifies the path by which the view can be accessed programmatically and by the URI fragment.

Java

  1. @SpringView(name="main")

If the view name is not given, it is derived from the class name by removing a possible “View” suffix, making it lower case, and using a dash (“-“) to separate words originally denoted by capital letters. Thereby, a view class such as MyFunnyView would have name “ my-funny”.

It is a recommended pattern to have the view name in a static member constant in the view class, as was done in the example previously, so that the name can be referred to safely.

You can also navigate to a view with a URI fragment such as #!myview/someparameter or programmatically with:

Java

  1. getUI().getNavigator().navigateTo("myview/someparameter");

The enter() method of the view gets the URI fragment as parameter as is and can interpret it in any application-defined way.

uis

If the application has multiple UIs that use SpringViewProvider, you can use this parameter to specify which UIs can show the view.

Java

  1. @SpringView(name="myview", uis={MySpringUI.class})

If the list contains UI.class, the view is available to all UIs.

Java

  1. @SpringView(name="myview", uis={UI.class})

In the following, we have a login view that accesses a session-scoped user object. Here, we use a constant to define the view name, so that we can use the constant when navigating to it.

Java

  1. @SpringView(name=LoginView.NAME)
  2. public class LoginView extends CustomComponent
  3. implements View {
  4. public final static String NAME = "";
  5. // Here we inject to the constructor and actually do
  6. // not store the injected object to use it later
  7. @Autowired
  8. public LoginView(User user) {
  9. VerticalLayout layout = new VerticalLayout();
  10. // An input field for editing injected data
  11. BeanItem<User> item = new BeanItem<User>(user);
  12. TextField username = new TextField("User name",
  13. item.getItemProperty("name"));
  14. username.setNullRepresentation("");
  15. layout.addComponent(username);
  16. // Login button (authentication omitted) / Java 8
  17. layout.addComponent(new Button("Login", e ->
  18. getUI().getNavigator().
  19. navigateTo(MainView.VIEWNAME)));
  20. setCompositionRoot(layout);
  21. }
  22. @Override
  23. public void enter(ViewChangeEvent event) {}
  24. }

You could now navigate to the view from any other view in the UI with:

Java

  1. getUI().getNavigator().navigateTo(LoginView.VIEWNAME);

Access Control

Access control for views can be implemented by registering beans implementing ViewAccessControl or ViewInstanceAccessControl, which can restrict access to the view either before or after a view instance is created.

Integration with authorization solutions, such as Spring Security, is provided by additional unofficial add-ons on top of Vaadin Spring.

Access Denied View

If access to a view is denied by an access control bean, the access denied view is shown for it. For non-existing views, the error view is shown. You can set up an “Access Denied” view that is shown if the access is denied with setAccessDeniedViewClass() in SpringViewProvider, and an error view with setErrorView() in SpringNavigator. The same view can also be used both as an access denied view and as an error view to hide the existence of views the user is not allowed to access.

Java

  1. @Autowired
  2. SpringViewProvider viewProvider;
  3. @Autowired
  4. SpringNavigator navigator;
  5. @Override
  6. protected void init(VaadinRequest request) {
  7. // Set up access denied view
  8. viewProvider.setAccessDeniedViewClass(
  9. MyAccessDeniedView.class);
  10. // Set up error view
  11. navigator.setErrorView(MyErrorView.class);

Note that the error view can also be a class with which an error view bean is found. In this case, the error view must be UI scoped.

Deploying Spring UIs and Servlets

Vaadin Spring hooks into Vaadin framework by using a special SpringVaadinServlet. As described earlier, you do not need to map an URL path to a UI, as it is handled by Vaadin Spring. However, in the following, we go through some cases where you need to customize the servlet or use Spring with non-Spring servlets and UIs in a web application.

Custom Servlets

When customizing the Vaadin servlet, as outlined in “Vaadin Servlet, Portlet, and Service”, you simply need to extend com.vaadin.spring.server.SpringVaadinServlet instead of com.vaadin.servlet.VaadinServlet.

  1. @WebServlet(value = "/*", asyncSupported = true)
  2. public class MySpringServlet extends SpringVaadinServlet {
  3. }

The custom servlet must not have @VaadinServletConfiguration, as you would normally with a Vaadin servlet, as described in “Deploying an Application”.

Defining Servlet Root

Spring UIs are managed by a Spring servlet ( SpringVaadinServlet), which is by default mapped to the root of the application context. For example, if the name of a Spring UI is “ my-spring-ui” and application context is /myproject, the UI would by default have URL “ /myproject/my-spring-ui”. If you do not want to have the servlet mapped to context root, you can use a @WebServlet annotation for the servlet or a web.xml definition to map all Spring UIs to a sub-path.

For example, if we have a root UI and another:

  1. @SpringUI(path="") // At Spring servlet root
  2. public class MySpringRootUI extends UI {…​}
  3. @SpringUI("another")
  4. public class AnotherUI extends UI {…​}

Then define a path for the servlet by defining a custom servlet:

  1. @WebServlet(value = "/myspringuis/*", asyncSupported = true)
  2. public class MySpringServlet extends SpringVaadinServlet {
  3. }

These two UIs would have URLs /myproject/myspringuis and /myproject/myspringuis/another, respectively.

You can also map the Spring servlet to another URL in servlet definition in web.xml, as described the following.

Mixing With Other Servlets

The SpringVaadinServlet is normally used as the default servlet, but if you have other servlets in the application, such as for non-Spring UIs, you need to define the Spring servlet explicitly in the web.xml. You can map the servlet to any URL path, but perhaps typically, you define it as the default servlet as follows, and map the other servlets to other URL paths:

  1. <web-app>
  2. …​
  3. <servlet>
  4. <servlet-name>Default</servlet-name>
  5. <servlet-class>
  6. com.vaadin.spring.server.SpringVaadinServlet
  7. </servlet-class>
  8. </servlet>
  9. <servlet-mapping>
  10. <servlet-name>Default</servlet-name>
  11. <url-pattern>/myspringuis/*</url-pattern>
  12. </servlet-mapping>
  13. <servlet-mapping>
  14. <servlet-name>Default</servlet-name>
  15. <url-pattern>/VAADIN/*</url-pattern>
  16. </servlet-mapping>
  17. </web-app>

With such a setting, paths to Spring UIs would have base path /myapp/myspringuis, to which the (optional) UI path would be appended. The /VAADIN/* only needs to be mapped to the servlet if there are no other Vaadin servlets.