Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is none scope bean and when to use it?

Tags:

java

scope

jsf

could some explain what a none scope is and purpose of it?

Suppose if i have a bean in

request scope as r1

session scope as s1

application scope a1

and say i inject none scope bean n1 in to each of above scopes then i find that n1 gets instantiated for each parent bean when ever its parent bean[r1/s1/a1] is instantiated.

none scope bean in a1 is available throughout in a1 since a1 is appl scope. none scope bean in s1 is available only until s1 is not destroyed and when s1 is created again n1 is instanciated and made available to it.

Is it correct?

and what the purpose of using it? only to avoid creating such bean our self?

many thanks

like image 673
jch Avatar asked Jun 21 '10 17:06

jch


People also ask

How do you decide what should be the scope of the bean?

You place managed beans into the application scope if a single bean should be shared among all instances of a web application. The bean is constructed when it is first requested by any user of the application, and it stays alive until the web application is removed from the application server.

In which scenario we will use singleton and prototype scope?

As a rule of thumb, you should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans.

What is the default bean scope attribute and what is its function?

Singleton is the default scope for a Bean, the one that will be used if nothing else is indicated. This scope implies that Spring container will create an only shared instance of the class designated by this bean, so each time the Bean is required the same object will be injected.

What is SessionScope in Spring?

@SessionScope is a specialization of @Scope for a component whose lifecycle is bound to the current web session. Specifically, @SessionScope is a composed annotation that acts as a shortcut for @Scope("session") with the default proxyMode() set to TARGET_CLASS .


1 Answers

A bean with a <managed-bean-scope> of none or a @NoneScoped annotation will be created on every single EL expression referencing the bean. It isn't been stored by JSF anywhere. The caller has got to store the evaluated reference itself, if necessary.

E.g. the following in the view

<p>#{noneScopedBean.someProperty}</p>
<p>#{noneScopedBean.someProperty}</p>
<p>#{noneScopedBean.someProperty}</p>

on a none-scoped bean will construct the bean 3 (three) times during a request. Every access to the bean gives a completely separate bean which is been garbaged immediately after the property access.

However, the following in for example a session scoped bean

@ManagedProperty("#{noneScopedBean}")
private NoneScopedBean noneScopedBean;

will make it to live as long as the session scoped bean instance. You should only make sure that you access it in the view by #{sessionScopedBean.noneScopedBean.someProperty} instead.

So it may be useful when you want scope-less data being available as a managed property in an arbitrary bean.

like image 175
BalusC Avatar answered Oct 16 '22 19:10

BalusC