From what I've been studying, I know there are several scopes for beans:
@RequestScoped
@ViewScoped
@FlowScoped
@SessionScoped
@ApplicationScoped
What is the function of each? How should I correctly choose the scope of my bean?
From what I've been studying, I know there are several scopes for beans:
@RequestScoped
@ViewScoped
@FlowScoped
@SessionScoped
@ApplicationScoped
What is the function of each? How should I correctly choose the scope of my bean?
The difference is basically in the duration of each.
@RequestScoped
Born and dies after each request, no status is saved. Should be used on pages that only send data for viewing and have no further action depending on this data in the backend, for example, listings;
@ViewScoped
It is born after each page opening, and it dies as soon as a change in context URL occurs. Should be used in cases where the editing of some data happens and the beans in the backend should stay alive so they can be saved later;
@FlowScoped
It is basically the same as @ViewScoped
but does not end with page exchange, you can define a group of pages that share the same scope, and end at a certain point. It can be used for registrations that last for more than one page for example;
@SessionScoped
It is born as soon as a session is established with the client and the bean in question is requested, it dies as soon as the session is finished. This bean has a very long duration and should not be used in most scenarios. It should only be used to save user session information that should last for a longer time;
@ApplicationScoped
It is born as soon as it is requested for the first time in the application, lasts while it is alive and is shared among the various clients. It should be used to do things in the application context, such as storing a number of logged-in clients (it's an idiotic example, but I just thought about this now);