Do you think IIT Guwahati certified course can help you in your career?
No
Introduction💁
Java is an object-oriented, class-based, high-level programming language. It is based on the concept of WORA (write once use anywhere) for developer ease. Java classes are compiled into bytecode. The compiled byte codes can be used on any platform supporting Java without recompiling the class.
Spring Scopes are services that are used to manage the lifecycle of various narrow objects. Spring Scopes are used to manage the lifecycle of a particular user session, a single request, or even a particular UI instance.
Scopes in Vaadin
The Vaadin Spring add-on comes with three additional scopes, namely.
VaadinSessionScope
UIScope
RouteScope
Vaadin Session Scope
The VaadinSessionScope is used to target the beans to a particular user’s session. Let us look at an example of VaadinSessionScope.
//creating a function for the root or main route
@Route("")
public class Firstlayout extends Div {
public Firstlayout(@Autowired SessionService bean) {
//Displaying the session id of the tab using the bean method.
setText(bean.getText());
}
}
//creating a function for the admin route
@Route("admin")
public class secondLayout extends Div {
public secondLayout(@Autowired SessionService bean) {
//Displaying the session id of the tab using the bean method.
setText(bean.getText());
}
}
//Creating the main function to handle the route calls
@SpringComponent
//Creating a vaadin session
@VaadinSessionScope
public class SessionService {
private String uid = UUID.randomUUID().toString();
public String getText(){
return "session " + uid;
}
}
You can also try this code with Online Java Compiler
In the above example, the same text is displayed if you open the main route in one tab and the admin route in another. This happens because of the fact that both session ids remain the same even though we are using two different tabs.
UI Scope
The UIScope is used to target the beans to a browser’s window or tab opened by the user. Let us look at an example of UIScope.
//creating a function for the root or main route
@Route("")
public class FirstLayout extends Div {
public MainLayout(@Autowired UIService bean) {
//Displaying the session id of the tab using the bean method.
add(new Span(bean.getText()));
add(new RouterLink("Open admin", SecondLayout.class));
}
}
//creating a function for the admin route
@Route("admin")
public class SecondLayout extends Div {
public Editor(@Autowired UIService bean) {
//Displaying the session id of the tab using the bean method.
add(new Span(bean.getText()));
add(new RouterLink("Open Root", FirstLayout.class));
}
}
//Creating the main function to handle the route calls
@SpringComponent
//Creating a UI Session
@UIScope
public class UIService {
private String uid = UUID.randomUUID().toString();
public String getText() {
return "ui " + uid;
}
}
You can also try this code with Online Java Compiler
In the above example, different texts are displayed if you open the root target in one tab and the admin tab in another. This happens because of the fact that the UI id is different for different tabs or windows. However, the text will remain the same if we navigate inside the same browser tab between the root and admin target.
Route Scope
The RouteScope is used to target the beans to a specific routing component or a specific route that is pointed out by the RouteScopeOwner method.
Let us look at an example of two child routes and one owner route.
@SpringComponent
@RouteScope
@RouteScopeOwner(Parent.class)
public class RouteService {
private String uid = UUID.randomUUID().toString();
public String getText() {
return "ui " + uid;
}
}
@Route("")
@RoutePrefix("parent")
public class Parent implements RouterLayout extends VerticalLayout{
public Parent(@RouteScopeOwner(ParentView.class) @Autowired RouteService routeService) {
add(new Span("Parent view:" + routeService.getText()),
new RouterLink("Open Child-A", FirstChild.class),
new RouterLink("Open Child-B", SecondChild.class),
new RouterLink("Open Sibling", SiblingView.class));
}
}
@Route(value = "child-a", layout = ParentView.class)
public class FirstChild extends VerticalLayout {
public FirstChild(@Autowired @RouteScopeOwner(ParentView.class) RouteService routeService) {
add(new Text("Child-a: " + routeService.getText()));
}
}
@Route(value = "child-b", layout = ParentView.class)
public class SecondChild extends VerticalLayout {
public SecondChild(@Autowired @RouteScopeOwner(ParentView.class) RouteService routeService) {
add(new Text("Child-a: " + routeService.getText()));
}
}
@Route(value = "sibling")
public class SiblingView extends VerticalLayout {
public SiblingView() {
add(new RouterLink("Open ParentView", Parent.class),
new RouterLink("Open Child-A", FirstChild.class),
new RouterLink("Open Child-B", SecondChild.class));
}
}
You can also try this code with Online Java Compiler
The need to preserve the beans often arises since, by default, all the routing components are recreated when the page is refreshed. Users can use the PreserveOnRefresh annotation to preserve the beans on refresh.
Let us look at an example of preserving the beans for the Route Scope.
@SpringComponent
@RouteScope
@RouteScopeOwner(MainLayout.class)
public class PreservedBean {
private String uid = UUID.randomUUID().toString();
public String getText() {
return uid;
}
}
@Route("")
@PreserveOnRefresh
public class MainLayout implements RouterLayout extends VerticalLayout{
public MainLayout( @RouteScopeOwner(ParentView.class) @Autowired PreservedBean bean) {
add(new Span("UID:" + bean.getText()));
}
}
You can also try this code with Online Java Compiler
However, the beans present in the UIScope can never be preserved. Users can only preserve the beans present in the Route Scope.
Now let's discuss some frequently asked questions associated with Vaadin Spring Scopes.
Frequently Asked Questions
What are Embedded Applications?
An embedded application is nothing but software placed inside another software to perform a particular task. In our case, we have embedded an application inside a web page.
What is Spring in Java?
Spring is an open-source framework in Java that is used for application and web development.
What are the key features of Spring in Java?
The key features of the Spring framework in Java are,
Inversion of Control Container
Data Access Framework
Support for Aspect-Oriented Programming
Transaction Management Framework
Spring Web Service
Spring MVC Framework, Spring Test Framework
What are some real-time uses of Embedded Applications?
Embedded Applications are widely used in many real-time applications, such as.
Smart TVs
Missile Guidance System
ABS, Airbags
Smart Phones
GPS Navigation Systems
Road Safety Systems
What is the need to preserve the content of an Embedded Application on refresh?
Since clicking refresh is not counted as a time-out scenario, so as soon as the reload button is pressed, this causes the existing subclass objects to be replaced with new subclass objects. Thus there might rise a need to preserve the content on hitting refresh.
Conclusion
This Blog covered all the necessary points about the Vaadin Spring Scopes. We also looked at various types of Spring Scopes present in Vaadin. We further discussed an example for each Spring Scope.
Hey Ninjas! Don’t stop here. Check out Coding Ninjas for Python, more unique courses, and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and excellent Machine Learning and Python problems.