taylordurden
taylordurden

Reputation: 357

Create SessionScoped version of ApplicationScoped bean

I have an @ApplicationScoped bean which I create a bunch of instances of. I then use those instances to create a JSF page with the data contained in the instances. I want the user to be able to modify those data (for thier use only, not stored), but not have it affect the view for other users who share access to the same bean. I'm considering creating a @SessionScoped version of the @ApplicationScoped bean for each new session, but am not sure what the best way to go about it is. Should I:

  1. Extend the @ApplicationScoped bean and give it @SessionScoped, then in my constructor, grab all of the relvant data?

  2. Not create a @SessionScoped version, but rather create a "user" version for each piece of data that the user can modify in the @ApplicationScoped bean?

  3. Other ideas?

Upvotes: 0

Views: 326

Answers (2)

Rodmar Conde
Rodmar Conde

Reputation: 956

I really recommend you to read this other question to improve your architecture design: Understanding JSF as a MVC framework

It seems you have started to code in the wrong sense. During design-phase of development, you should first think about @sessionscoped beans that your application will need (something as thinking in your users-needs). Then, you should thing about the @applicationscoped beans (like your global-application-needs).

It is really strange to build a @sessionscoped bean extending it from an @applicationscoped bean.

You should write a @sessionscoped with all the necessary data and initialize it with relevant data, as you said. You will still be able to access @applicationscoped method's. Anyway, try to minimize your access to @apllicationscoped beans, code into @sessionscoped bean as much as possible.

Regards,

Upvotes: 1

user2880020
user2880020

Reputation:

I have an @ApplicationScoped bean which I create a bunch of instances of.

That's wrong , the real purpose of an @ApplicationScoped bean is to minimize data redundancy in the JVM's Heap memory. There should be only one instance at any time, that one instance is always accessible by any other scoped beans. As suggested by Luiggi Mendoza above, you can go for a @SessionScoped or @ViewScoped bean, depending on your use case.

But simply, never use an @ApplicationScoped bean if the data is not going to be shared or persisted.

Upvotes: 2

Related Questions