0

I am having problems understanding a few scope concepts.

Let's say I have this Bean (used in a xhtml):

@Named
@RequestScoped
public class ConfigController implements Serializable {

    @Inject
    ConfigModel config; // Bean is @SessionScoped
}

If a ConfigController is created for each http request, what about the injected ConfigModel inside ? Does it keep its values, or is it recreated with the controller ? (I doubt it ?)

What if a ConfigModel is injected in multiple beans with different scopes ?

As I gathered, the scope hierarchy should go from broad to narrow: - Models should be session scoped, since they contain the data - Controllers (and services below) can be request scoped, since they only implement logic.

This is the original problem I am facing: @SessionScoped CDI bean is a different Instance when injected

Community
  • 1
  • 1
Tim
  • 3,910
  • 8
  • 45
  • 80
  • The ConfigModel would be new for each Session if it is Session Scoped. If you want to go from narrow to broad you can use an [Instance](https://docs.jboss.org/cdi/api/1.1/javax/enterprise/inject/Instance.html) `Object.` – Shaun Wild Aug 01 '16 at 09:14
  • _"If a ConfigController is created for each http request, what about the injected ConfigModel inside ? Does it keep its values, or is it recreated with the controller ? (I doubt it ?)"_ Try... And yes this normally works. – Kukeltje Aug 01 '16 at 09:14
  • 1
    Since someone marked it as duplicate before I could post answer, I'll at least add a simple comment. As long as you are within the same **session**, you get to inject the same `@SessionScoped` bean (`ConfigModel` in your case). Once you, for example, `invalidate()` that session and start sending new requests (hence creating new session), all the requests will get a newly created session injected. The original problem which led you here might have been caused by creating several sessions, but that is just my guess. – Siliarus Aug 01 '16 at 10:12
  • @BalusC is it now? I was answering the part when OP asks "Does it keep its values, or is it recreated with the controller ?". So nope, it is an aswer to this one with a hint to the original one. – Siliarus Aug 01 '16 at 12:03

0 Answers0