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
@Route("")
public class MainLayout extends Div {
public MainLayout(@Autowired SessionService bean){
setText(bean.getText());
}
}
@Route("editor")
public class Editor extends Div {
public Editor(@Autowired SessionService bean){
setText(bean.getText());
}
}
@Component
@VaadinSessionScope
public class SessionService {
private String uid = UUID.randomUUID().toString();
public String getText(){
return "session "+uid;
}
}
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
@Route("")
public class MainLayout extends Div {
public MainLayout(@Autowired UIService bean){
setText(bean.getText());
}
}
@Route("editor")
public class Editor extends Div {
public Editor(@Autowired UIService bean){
setText(bean.getText());
}
}
@Component
@UIScope
public class UIService {
private String uid = UUID.randomUUID().toString();
public String getText(){
return "ui " + uid;
}
}
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
public void edit() {
getUI().get().navigate("editor");
}
So inside the same UI
instance the same bean instance with @UIScope
is used.