title: @Inject annotation for object and two different behaviors of this object
body: <p>I use a JSF 2.2 and CDI. I created below example to show you a heart of my problem. You can see below:</p>
  
  <ul>
  <li>the two xhtml pages,</li>
  <li>the session scoped CDI bean (<code>LoginController</code>) which is the controller for the xhtml page1,</li>
  <li>the view scoped CDI bean (<code>DataController</code>) which is controller for the xhtml page2,</li>
  <li>the request scoped CDI bean (<code>DataServiceImpl</code>) which is the service for the<code>DataController</code> bean.</li>
  </ul>
  
  <p>This is a use case and flow of the data:</p>
  
  <p>xhtml page2 -> the <code>DataController</code> controller -> call <code>dataService.addData()</code> in <code>send()</code> method (which is located in the <code>DataController</code> class) -> execute <code>addData()</code> method (which is located in the <code>DataServiceImpl</code> class)</p>
  
  <p>The heart of my problem:</p>
  
  <ul>
  <li>If I inject the <code>LoginController</code> to the <code>DataServiceImpl</code> class, the <code>loginController</code> object doesn't work (i.e. it returns <code>null</code>) [Option 1 in the code below].</li>
  <li>If I inject the <code>LoginController</code> to the <code>DataController</code> class, the <code>loginController</code> object works fine (i.e. it returns what I want) [Option 2 in the code below].</li>
  </ul>
  
  <p>My question is: Why this injected object behaves in two different ways depending on place of injection?</p>
  
  <p>This is the part of my xhtml page1:</p>
  
  <pre><code>&lt;h:form&gt;               
  
      &lt;p:growl id="growl" showDetail="false" /&gt;
  
      &lt;h:inputText id="username" value="#{userLogin.username}" label="Username" required="true" requiredMessage="Username: This field is required." title="Enter your username." pt:placeholder="Username" /&gt;
      &lt;h:inputSecret  id="password" value="#{userLogin.password}" label="Password" required="true" requiredMessage="Password: This field is required." title="Enter your password." pt:placeholder="Password" /&gt;
      &lt;p:commandButton value="Login" action="#{loginController.login}" update="growl" /&gt;                  
  
  &lt;/h:form&gt;
  </code></pre>
  
  <p>This is the part of my xhtml page2:</p>
  
  <pre><code>&lt;h:form&gt;    
  
      &lt;p:growl id="growl" showDetail="false" /&gt;
  
      &lt;p:panelGrid id="panel" columns="2" styleClass="ui-noborder" columnClasses="rightalign,leftalign"&gt;
          &lt;p:outputLabel for="data" value="Data:" /&gt;
          &lt;p:inputText  id="data" value="#{data.text}" required="true" requiredMessage="Data: This field is required." /&gt;
          &lt;p:commandButton id="buttonSend" value="Send" action="#{dataController.send()}" update="messages" /&gt;                        
      &lt;/p:panelGrid&gt;                                              
  
  &lt;/h:form&gt;
  </code></pre>
  
  <p>This is the session scoped CDI bean:</p>
  
  <pre><code>@Named
  @SessionScoped
  public class LoginController implements Serializable {
  
      private static final long serialVersionUID = -6322113716363932422L;
  
      public String login(){      
  
          if(userService.login(userLogin)){
  
              currentUser=userService.getCurrnetUser(userLogin.getUsername());
              return "home?faces-redirect=true";          
          }
          else{
  
              facesContext.addMessage(null, new FacesMessage("Data entered are incorrect"));
              return null;
          }
      }
  
      public String logout(){
  
          currentUser=null;
          return "login?faces-redirect=true";
      }
  
      public boolean isLoggedIn() {
  
            return currentUser!=null;
      }
  
      @Produces
      @LoggedIn
      public UserAccount getCurrentUser(){
  
          return currentUser; 
      }   
  
      @Inject
      private FacesContext facesContext;
  
      @Inject
      private UserServiceImpl userService;
  
      @Named
      @Produces
      @RequestScoped
      private UserAccount userLogin=new UserAccount();
  
      private UserAccount currentUser;
  }
  </code></pre>
  
  <p>This is the view scoped CDI bean:</p>
  
  <pre><code>@Named
  @ViewScoped
  public class DataController implements Serializable {
  
      private static final long serialVersionUID = 1383572529241805730L;
  
      public void send(){
  
          /* OPTION 2
           * If I inject the LoginController here 
           * instead of in DataServiceImpl bean, the loginController object 
           * works fine (i.e. it isn't null and returns the name of the user). 
           */
          String name=loginController.getCurrentUser().getName();
  
          dataService=new DataServiceImpl();
          dataService.addData(data.getText);
      }
  
      @Named
      @Produces
      @RequestScoped
      private Data data=new Data();  
  
      @Inject
      private DataService dataService;
  
      @Inject
      private LoginController loginController;    
  }
  </code></pre>
  
  <p>This is the request scoped CDI bean:</p>
  
  <pre><code>@Named
  @RequestScoped
  public class DataServiceImpl implements DataService {
  
      @Override
      public void addData(String data) {
  
          /* OPTION 1
           * If I inject the LoginController here 
           * instead of DataController bean, the loginController object 
           * doesn't work (i.e. returns null. I get the NullPointerException exception 
           * in line below due to the loginController object which is null)
           */
          String name=loginController.getCurrentUser().getName();
  
          //Proccess some data
      }
  
      @Inject
      private LoginController loginController;
  }
  </code></pre>