-
1. Re: Is it possible to inject a session scoped bean optionally?
nickarls Jul 7, 2010 5:54 AM (in response to ruimo.ruimo.ruimo.com)Not exactly. Perhaps you can use some combination of a flag and a producer method?
-
2. Re: Is it possible to inject a session scoped bean optionally?
ruimo.ruimo.ruimo.com Jul 12, 2010 5:10 AM (in response to ruimo.ruimo.ruimo.com)Thank you for your reply. I eventually gave up to use @SessionScoped and have created the following producer method.
@RequestScoped @Produces LoginSession factory() { HttpSession hs = (HttpSession)context.getSession(false); if (hs == null) return LoginSession.NULL; try { return (LoginSession)hs.getAttribute(LoginSession.KEY); } catch (IllegalStateException e) { return LoginSession.NULL; } }
Where LoginSession is a class that holds login information:
@Named @RequestScoped public class LoginSession implements Serializable { public static final LoginSession NULL = new Null(); static class Null extends LoginSession { private static final long serialVersionUID = -8378908668473484687L; Null() { super (-1L); } @Override public boolean isLogined() {return false;} @Override public long getLoginId() { throw new IllegalStateException("User is not logged in."); } } final long loginId; public LoginSession(long loginId) { this.loginId = loginId; } public boolean isLogined() {return true;} public long getLoginId() {return loginId;} }
But WELD complains the following:
org.jboss.weld.exceptions.UnproxyableResolutionException: WELD-001400 Normal scoped bean org.jboss.weld.bean-flat-ProducerMethod-gojira.web.Session.method factory() is not proxyableAny suggestions will be appreciated.
-
3. Re: Is it possible to inject a session scoped bean optionally?
ruimo.ruimo.ruimo.com Jul 12, 2010 5:26 AM (in response to ruimo.ruimo.ruimo.com)Solved. I changed the scope of the class that declars factory() method to dependent. Then let the Null class to normal top-level request scoped bean.
Anyway, IMO, @SessionScoped seems to be not very convenient. -
4. Re: Is it possible to inject a session scoped bean optionally?
wangliyu Jul 27, 2010 3:27 PM (in response to ruimo.ruimo.ruimo.com)I found the same issue, here is a workaround:
@Named
@SessionScoped
public class LoginSession implement Serializable {
private User user;
public void login() {}@Produces
@RequestScoped
public User getUser() {}@Predestroy
public void logout() {}// called when request scope destroyed.
public void disposeUser(@Disposed User user) {}
}In other small scope you can just use:
@RequestScoped
@Named
public class someService {
@Inject private User user;
...
}this works
-
5. Re: Is it possible to inject a session scoped bean optionally?
wangliyu Jul 30, 2010 8:47 AM (in response to ruimo.ruimo.ruimo.com)Actually I'm thinking to use @Produces can do this,
@Produces
@SessionScoped
public LoginSession getLoginSession() {
if (ExternalContext.getSession(false) == null)
return null;
return new LoginSession();
}
but seems the getSession method always return not null.