what is none scope bean and when to use it?
Asked Answered
O

3

13

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

Odysseus answered 21/6, 2010 at 17:24 Comment(1)
Where did you read about "none scope" ?Piane
F
20

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.

Fremd answered 21/6, 2010 at 17:49 Comment(4)
i didn't get the benefit you have mentioned ...could you please elaborate a bit...Ophiology
@deadlus: by coincidence, I just started to play somewhat with it after I posted the answer. Sorry, it didn't work as I expected and I don't see any benefits anymore.Fremd
@Fremd " this scope may be useful when you'd like to define and declare managed properties in the faces-config.xml rather than directly in the parent bean itself." The parent bean doesn't allow to declare the scope of a managed property though annotation ?Trundle
@Fremd "it also gives more easy control to change the bean implementation of the managed property (if declared as an abstract/interface type)." How does it give more control ?Trundle
M
7

I'm using @nonescoped when my "view logic" dont need to be in any scope but be referenced by another ManagedBean.

I'm working with Liferay, as I want to make my architecture and design independent of liferay, I create my services interfaces and Dto, but when you need to persistence data, Liferay need that the companyId and companyGroupId be sended from the view layer (in this case JSF).

To maintain independence, I did a "Adapter pattern" creating a ServiceLayer ManagedBean with @noneScope with an interface independent from Liferay. This way I can get the companyId and the companyGroupId needed by the Liferay Apis.

The advantage of using @noneScope is that you can use it as a @ManagedProperty in any bean of any scope.

Majesty answered 10/10, 2012 at 13:27 Comment(0)
A
0

@NoneScoped would be beneficial in the following scenario.

Assume that we have to inject the same bean in two different scoped beans, we can mark that bean as @NoneScoped. Say a bean BeanOne with @NoneScoped can be easily injected in any bean with any scope like @Request or @Session.

Without using @NoneScoped for BeanOne, we may have to duplicate the bean with different scopes and inject them accordingly.

Anticipation answered 10/1, 2015 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.