title: @Named + @RequestScoped not working in JSF 2.0 with JBoss 7.1.1
body: <p>I have a working <code>@ManagedBean</code> which I'd like to substitute with a <code>@Named + @RequestScoped</code> bean. </p>
  
  <pre><code>// Before
  @ManagedBean
  public class Login {
     ...
  }
  
  // After
  import javax.enterprise.context.RequestScoped;
  @Named
  @RequestScoped
  public class Login {
     ...
  }
  </code></pre>
  
  <p>Everything works fine as long as I use <code>@ManagedBean</code>. <code>@Named</code> without <code>@RequestScoped</code> works but creates a new instance for each EL-expression. <code>@Named + @RequestScoped</code> however yields an exception: <strong>Unable to add request scoped cache item when request cache is not active</strong></p>
  
  <pre><code>java.lang.IllegalStateException: Unable to add request scoped cache item when request cache is not active
      at org.jboss.weld.context.cache.RequestScopedBeanCache.addItem(RequestScopedBeanCache.java:51)
      at de.prosis.dafe.presentation.Login$Proxy$_$$_WeldClientProxy.getUsername(Login$Proxy$_$$_WeldClientProxy.java)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
      at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
      at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
      at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
      at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
      at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
      at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
      at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
      ...
  </code></pre>
  
  <p>Empty <code>beans.xml</code> does exist. Are there any gotchas or bugs that I'm not aware of? I bet I'm missing something but it does look like a bug. I googled the exception message but haven't found anything but the source of the class which raises it. Thanks in advance!</p>
  
  <p><strong>Update:</strong>
  It is related with code I omitted. The login bean tries to invalidate the session in its constructor, which fails if it is a named bean (not always, if I spam F5 it seems to works after a few times) and works as a managed bean. Can somebody explain that behaviour?</p>
  
  <pre><code>public Login() {
              HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
              if (session != null) {
                  session.invalidate();
              }
  } 
  </code></pre>
  
  <p>Code comes from that tutorial:
  <a href="http://www.greenkode.com/2011/09/user-authentication-and-authorization-using-jaas-and-servlet-3-0-login/" rel="noreferrer">http://www.greenkode.com/2011/09/user-authentication-and-authorization-using-jaas-and-servlet-3-0-login/</a></p>