title: Injecting FacesContext with CDI
body: <p>I have my Bean:</p>
  
  <pre><code>import java.io.Serializable;
  
  import javax.enterprise.context.SessionScoped;
  import javax.faces.application.FacesMessage;
  import javax.faces.context.FacesContext;
  import javax.inject.Inject;
  import javax.inject.Named;
  import javax.persistence.EntityManager;
  
  import br.com.dropper.web.dao.UsuarioDAO;
  import br.com.dropper.web.model.Usuario;
  import br.com.dropper.web.util.JpaUtil;
  
  @Named
  @SessionScoped
  public class LoginBean implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Inject
      private FacesContext context;
  
      @Inject
      private Usuario usuario;
  
  
      //TODO: Persistencia e Transacao controladas por EJB
      private EntityManager em = new JpaUtil().getEntityManager();
      private UsuarioDAO usuarioDAO = new UsuarioDAO(em);
  
  
  
      public Usuario getUsuario() {
          return usuario;
      }
  
      public String autenticar() {
  
          Usuario usuario = usuarioDAO.obterUsuarioPorEmail(this.usuario);
  
          if (usuario == null) {
              context.addMessage(null, new FacesMessage("Usuario não encontrado."));
              context.getExternalContext().getFlash().setKeepMessages(true);
              return "login?faces-redirect=true";
          } else {
              context.getExternalContext().getSessionMap().put("usuarioLogado", usuario);
              return "dashboardImagem.xhtml?faces-redirect=true";
          }
      }
  
      public String cadastrarUsuario() {
          System.out.println("Redirecionando para cadastroUsuario.xhtml");
          return "cadastroUsuario.xhtml?faces-redirect=true";
      }
  
      public String logout() {
          context.getExternalContext().getSessionMap().remove("usuarioLogado");
          context.getExternalContext().invalidateSession();
          return "login.xhtml?faces-redirect=true";
  
      }
  
  }
  </code></pre>
  
  <p>And my Factory:</p>
  
  <pre><code>package br.com.dropper.web.factory;
  
  import java.io.Serializable;
  
  import javax.enterprise.inject.Produces;
  import javax.faces.context.FacesContext;
  import javax.faces.view.ViewScoped;
  
  public class FacesContextFactory implements Serializable{
  
      private static final long serialVersionUID = 1L;
  
      @Produces
      @ViewScoped
      public FacesContext getFacesContext(){
          return FacesContext.getCurrentInstance();
      }
  
  }
  </code></pre>
  
  <p>When i run my application, im getting this exception:</p>
  
  <pre><code>Caused by: org.jboss.weld.exceptions.IllegalProductException: WELD-000053: Producers cannot declare passivating scope and return a non-serializable class: Producer for Producer Method [FacesContext] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces @ViewScoped public br.com.dropper.web.factory.FacesContextFactory.getFacesContext()] declared on Managed Bean [class br.com.dropper.web.factory.FacesContextFactory] with qualifiers [@Any @Default]
      at br.com.dropper.web.factory.FacesContextFactory.getFacesContext(FacesContextFactory.java:16)
  </code></pre>
  
  <p>And when i change the produces method to @RequestScoped, my FacesContext it's inject only one time, and in my second page xhtml, i get a nullpointer =(</p>
  
  <p>UPDATE: pom</p>
  
  <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;br.com.dropper&lt;/groupId&gt;
      &lt;artifactId&gt;dropper-web&lt;/artifactId&gt;
      &lt;version&gt;0.1&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;dependencyManagement&gt;
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;javax&lt;/groupId&gt;
                  &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
                  &lt;version&gt;7.0&lt;/version&gt;
                  &lt;scope&gt;provided&lt;/scope&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
      &lt;/dependencyManagement&gt;
  
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax&lt;/groupId&gt;
              &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- &lt;dependency&gt;
              &lt;groupId&gt;dropper-web&lt;/groupId&gt;
              &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
              &lt;version&gt;9.4.1212&lt;/version&gt;
              &lt;scope&gt;system&lt;/scope&gt;
              &lt;systemPath&gt;${project.basedir}/src/main/webapp/WEB-INF/lib/postgresql-9.4.1212.jre6.jar&lt;/systemPath&gt;
          &lt;/dependency&gt;  --&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;dropper-web&lt;/groupId&gt;
              &lt;artifactId&gt;bootstrap&lt;/artifactId&gt;
              &lt;version&gt;1.0.10&lt;/version&gt;
              &lt;scope&gt;system&lt;/scope&gt;
              &lt;systemPath&gt;${project.basedir}/src/main/webapp/WEB-INF/lib/bootstrap-1.0.10.jar&lt;/systemPath&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;dropper-web&lt;/groupId&gt;
              &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
              &lt;scope&gt;system&lt;/scope&gt;
              &lt;systemPath&gt;${project.basedir}/src/main/webapp/WEB-INF/lib/commons-io-2.5.jar&lt;/systemPath&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;dropper-web&lt;/groupId&gt;
              &lt;artifactId&gt;cupertino&lt;/artifactId&gt;
              &lt;version&gt;1.0.10&lt;/version&gt;
              &lt;scope&gt;system&lt;/scope&gt;
              &lt;systemPath&gt;${project.basedir}/src/main/webapp/WEB-INF/lib/cupertino-1.0.10.jar&lt;/systemPath&gt;
          &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;dropper-web&lt;/groupId&gt;
              &lt;artifactId&gt;primefaces-6.0&lt;/artifactId&gt;
              &lt;version&gt;6.0&lt;/version&gt;
              &lt;scope&gt;system&lt;/scope&gt;
              &lt;systemPath&gt;${project.basedir}/src/main/webapp/WEB-INF/lib/primefaces-6.0.jar&lt;/systemPath&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
  
      &lt;build&gt;
          &lt;finalName&gt;dropper-web&lt;/finalName&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.8&lt;/source&gt;
                      &lt;target&gt;1.8&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Att</p>