Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default Managed Bean Scope in a JSF 2 application?

Tags:

jsf

Normally the default scope is the Request scope. I ve tried to delete the scope annotation and expected the bean to have a request bean scopped behaviour (by returning a submitted value on a previous page, I remember i've tried this the past and it worked fine) but i got nothing on the expression language wired to it. So what is the default scope and why it's not the same behaviour?!

like image 857
Bardelman Avatar asked Oct 11 '13 15:10

Bardelman


People also ask

Which one is a bean scope in JSF managed bean?

The scope of the managed bean is declared using one of NoneScoped , RequestScoped , ViewScoped , SessionScoped , ApplicationScoped , or CustomScoped annotations. If the scope annotations are omitted, the bean must be handled as if the RequestScoped annotation is present.

What are scopes in JSF?

The session scope allows you to create and bind objects to a session. It gets created upon the first HTTP request involving this bean in the session and gets destroyed when the HTTP session is invalidated. The request scope is present in JSF and CDI and functions in the same way.

What is the default scope of a managed bean?

Normally the default scope is the Request scope.

What scopes are available for a managed bean?

You can use following scopes for a bean class: Application (@ApplicationScoped): Application scope persists across all users? interactions with a web application. Session (@SessionScoped): Session scope persists across multiple HTTP requests in a web application.


1 Answers

Depends on who's managing the bean.

If it's JSF via @ManagedBean, then it defaults to @RequestScoped, as mentioned in the javadoc:

If the scope annotations are omitted, the bean must be handled as if the RequestScoped annotation is present

If it's CDI via @Named, then it defaults to @Dependent, as mentioned in Weld documentation:

Finally, CDI features the so-called dependent pseudo-scope. This is the default scope for a bean which does not explicitly declare a scope type.

The effect is that the bean instance is newly created on every single EL expression. So, imagine a login form with two input fields referring a bean property and a command button referring a bean action, thus with in total three EL expressions, then effectively three instances will be created. One with the username set, one with the password set and one on which the action is invoked. In effects, this behaves the same as JSF @NoneScoped. This confirms the symptoms you're seeing.

If it's Spring via @Component, then it defaults to @Scope("singleton"), as mentioned in javadoc:

Default: "singleton"

In effects, this behaves the same as JSF @ApplicationScoped and CDI @ApplicationScoped.

Netbeans has got nothing to do with it. It's just an editing tool like notepad but then with 1000+ extra features.

See also:

  • what is none scope bean and when to use it?
  • How to choose the right bean scope?
like image 119
BalusC Avatar answered Sep 18 '22 09:09

BalusC