简体   繁体   中英

JSF Static Bean Variable Scope

So, I've seen this post: JSF - session scoped bean shared by browsers on different machines

But this was a question from two years ago, so I don't know if there have been any updates in the world of JSF since then, and I also have some more specific cases to which I'd seek clarification. Basically, what I'd like to know is how static scope variables are handled in beans with different scopes. For example:

@ManagedBean
@ApplicationScoped
public class ApplicationBean{
    static private int someStaticInt=0;

    ...
}

Since this bean is Application scoped, I would completely expect someStaticInt to be shared by all users of the app, ie user A sets the value to 3, all users would henceforth see that value as 3. Correct me if I'm wrong.

But what about this scenario:

@ManagedBean
@ViewScoped
public class ViewScopeBean{
     static private int staticInt = 0;
     private SomePOJO myClass;
     ...

     public void someAction(){
         SomePOJO.memberStaticInt++;
         ...
     }
}

...

public SomePOJO{
    static private int memberStaticInt = 0;
    ...
}

Now, this bean is ViewScoped, so there's a separate instance for each user of the application. But what about that static int? If I increment that, is it only going to be within MY instance of the Bean, or is it going to be incremented for all users. Furthermore, what about that member object, myClass? It's not declared static in the bean, but it has a static member itself. If I run someAction, will memberStaticInt be incremented for all users or just the user using that instance of the Bean?

Finally, I'd be interested to know if any and all such logic in the above cases also applies to RequestScoped beans.

JSF Scopes won't change the meaning of static. Static still means static so regardless of your JSF scope that value will be shared by all instances of that class within the same VM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM