Ok, here's a workaround. (Mind you, I'm saying that it's a [i]workaround[/i], not a proper [i]solution[/i])
1. Create another bean which injects the necessary @SessionScoped bean. Make it a @Named bean, because we'll need to access it via EL in step 2.
[code]
@RequestScoped
@Named
public class SomeBean {
@Inject UserState userState;
public UserState getUserState() {
return userState;
}
}
[/code]
2. Then, inside the PhaseListener, instantiate that new bean via JSF ([i]not[/i] directly, so that injection would work), and then obtain the @SessionScoped bean via the getter:
[code]
FacesContext ctx = FacesContext.getCurrentInstance();
SomeBean sb = ctx.getApplication().evaluateExpressionGet(ctx, "#{someBean}", SomeBean.class);
UserState userState = sb.getUserState();
[/code]
So, now we have the @SessionScoped UserState instance that we need.
However, I still don't think that a framework such as CDI should force us to use hacks and workarounds for [i]something as simple as that[/i]. If the normal cases are such a PITA, what shall we do when we hit a [i]real[/i] snag?
I admit I don't know every detail of CDI but, judging from the lack of replies, probably my question didn't have a good answer. I would have expected that somebody would show me programmatic API that I could use to retrieve my bean. For example, API that would ask me to supply things like scope (optional), bean type and qualifiers, and then would give me the bean that matches the criteria.
Please, anyone, could you tell me if I can exert more control over CDI? As it stands, I feel extremely reluctant to use it because I'm biting my nails even for the simple scenarios. I'm not willing to take a chance with it for the big stuff.
CDI has good points indeed, but above all I need [i]control[/i]. I need to be able to use it [i]only when I need it[/i]. For example, to have a [i]service[/i] interface and to request an implementation of that interface, similar to OSGi's services but with scopes. Or like the NetBeans Lookup framework with scopes. Or like Guice's Injector but with scopes: cdiInjector.getInstance([SessionScoped.class,] MyServiceInterface.class, qualifiers). Thus, I could have objects that are not under CDI's control (like JSF PhaseListeners), and from them to request CDI beans [i]only when I need them[/i]. At the moment, it looks like "all or nothing" -- either I will use CDI for everything, or I should not use it all.
But maybe I'm wrong... Please, prove me wrong. I'll be happy to find out that CDI gives me more control.
[Message sent by forum member 'vesuvius' (vesuvius_prime_at_hotmail.com)]
http://forums.java.net/jive/thread.jspa?messageID=388604