Vaadin Spring Scopes

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 or even just a single request. Such a context defines the lifecycle of the object: its creation, use, and finally its destruction.

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.

In addition to standard Spring scope the Spring add-on introduces two new scopes: VaadinSessionScope and UIScope.

VaadinSessionScope manages the Spring beans during Vaadin Session lifecycle. It means that the same bean instance will be used within the whole Vaadin session.

Note
Please refer to User Session section of Application Lifecycle tutorial for details about the Vaadin Session lifecycle.

Java

  1. @Route("")
  2. public class MainLayout extends Div {
  3. public MainLayout(@Autowired SessionService bean){
  4. setText(bean.getText());
  5. }
  6. }
  7. @Route("editor")
  8. public class Editor extends Div {
  9. public Editor(@Autowired SessionService bean){
  10. setText(bean.getText());
  11. }
  12. }
  13. @Component
  14. @VaadinSessionScope
  15. public class SessionService {
  16. private String uid = UUID.randomUUID().toString();
  17. public String getText(){
  18. return "session "+uid;
  19. }
  20. }

In this example the same instance of SessionService will be used as long as we access the application from the same Vaadin session since it’s session scoped. E.g. if you open the root target in one tab and the editor target in another tab, then the shown text will be the same for both. It happens because the session is the same even though the tabs (and UI instances) are different.

The @UIScope manages the Spring beans during the UI lifecycle. Similar to the example above the UIService will be the same while we are in the same UI since it’s ui scoped now.

Note
Please refer to Loading a UI section of Application Lifecycle tutorial for details about the UI lifecycle.

Java

  1. @Route("")
  2. public class MainLayout extends Div {
  3. public MainLayout(@Autowired UIService bean){
  4. setText(bean.getText());
  5. }
  6. }
  7. @Route("editor")
  8. public class Editor extends Div {
  9. public Editor(@Autowired UIService bean){
  10. setText(bean.getText());
  11. }
  12. }
  13. @Component
  14. @UIScope
  15. public class UIService {
  16. private String uid = UUID.randomUUID().toString();
  17. public String getText(){
  18. return "ui " + uid;
  19. }
  20. }

Now if you open two browser tabs, the text in these will be different since the UI instances are different. But if you navigate to the Editor instance via the router (or the UI instance which delegates navigation to the router) then the text will be the same.

Java

  1. public void edit() {
  2. getUI().get().navigate("editor");
  3. }

So inside the same UI instance the same bean instance with @UIScope is used.