4 Replies Latest reply on Aug 31, 2010 5:58 AM by Nicklas Karlsson

    Beans @Inject returns null

    nimo stephan Master

      I use this (from the Weld-Login-Example):




      @Named @RequestScoped
      public class Credentials {
      
              private String username;
              @NotNull @Size(min=3, max=25)
              public String getUsername() { return username; }
              public void setUsername(String username) { this.username = username; }
      
              private String password;
              @NotNull @Size(min=3, max=25)
              public String getPassword() { return password; }
              public void setPassword(String password) { this.password = password; }
      
      }



      and my login-code:



      @SessionScoped @Named
      public class Login{
      
              @PersistenceContext EntityManager entityManager;
              @Inject Credentials credentials;
      
              private User user;
      
              public void login() {
      
                      @SuppressWarnings("unchecked")
                      List<User> results = (List<User>) entityManager
                                      .createQuery("select u from User u where u.username = :username and u.password = :password")
                                      .setParameter("username", credentials.getUsername())
                                      .setParameter("password", credentials.getPassword())
                                      .getResultList();
                      
      
                      System.out.println("Username: "+ credentials.getUsername());
                      System.out.println("Password: "+ credentials.getPassword());
                      
                      System.out.println("!results.isEmpty(): "+ !results.isEmpty());
      
      
                      if (!results.isEmpty()) {
                              user = results.get(0);
                              
                              FacesContext.getCurrentInstance().addMessage(null,
                                              new FacesMessage("Welcome, " + user.getUsername()));
                              
                              System.out.println("User found: "+ user.getUsername());
                      }
      
                      else {
                              FacesContext.getCurrentInstance().addMessage(null,
                                              new FacesMessage("This User does not exist."));
                      }
              }




      my view:




      <h:form>
              
              <div style="color: red">
                      <h:messages id="messages" globalOnly="true" />
              </div>
              
              
              <h:panelGrid columns="3" rendered="#{!login.loggedIn}">
                      <f:validateBean>
                              <h:outputLabel for="username">Username:</h:outputLabel>
                              <h:inputText id="username" value="#{credentials.username}" />
                              <h:message for="username"/>
                              <h:outputLabel for="password">Password:</h:outputLabel>
                              <h:inputSecret id="password" value="#{credentials.password}" />
                              <h:message for="password"/>
                      </f:validateBean>
              </h:panelGrid>
      
              <h:commandButton value="Login" action="#{login.login}"
                      rendered="#{!login.loggedIn}" />
      
              <h:commandButton value="Logout" action="#{login.logout}"
                      rendered="#{login.loggedIn}" />
      
      </h:form>



      When clicking the Button Login, the console prints this:



      
      11:09:37,820 INFO  [STDOUT] Hibernate: 
      11:09:37,820 INFO  [STDOUT]     select
      11:09:37,820 INFO  [STDOUT]         user0_.ID_USER as ID1_11_,
      11:09:37,820 INFO  [STDOUT]         user0_.firstname as firstname11_,
      11:09:37,820 INFO  [STDOUT]         user0_.lastname as lastname11_,
      11:09:37,820 INFO  [STDOUT]         user0_.password as password11_,
      11:09:37,820 INFO  [STDOUT]         user0_.username as username11_ 
      11:09:37,820 INFO  [STDOUT]     from
      11:09:37,820 INFO  [STDOUT]         SCHEMA.USER user0_ 
      11:09:37,820 INFO  [STDOUT]     where
      11:09:37,820 INFO  [STDOUT]         user0_.username=? 
      11:09:37,820 INFO  [STDOUT]         and user0_.password=?
      
      11:09:37,883 INFO  [STDOUT] Username: null
      
      11:09:37,883 INFO  [STDOUT] Password: null
      
      11:09:37,883 INFO  [STDOUT] !results.isEmpty(): false




      So you see, the injection of Credentials in Bean Login does not work.


      Why?



      (I am using JBOSS 6 M4 with actual CDI, JSF2).

        • 1. Re: Beans @Inject returns null
          nimo stephan Master

          I guess, I found the reason, why injected Instance of Credentials returns null:



          When rendering the page login, then my debug-console views this:




          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Credentials was created.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Loginwas created.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Credentials was created.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Loginwas created.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Credentials was created.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Loginwas created.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Credentials was created.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Loginwas created.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Credentials was created.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Loginwas created.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Credentials was created.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Credentials was created.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Credentials was created.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Loginwas created.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Credentials was created.
          
          11:39:43,430 INFO  [STDOUT] @PostConstruct
          
          11:39:43,430 INFO  [STDOUT] Loginwas created.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,430 INFO  [STDOUT] @PreDestroy
          
          11:39:43,430 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Credentials was created.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Loginwas created.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Credentials was created.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Loginwas created.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Credentials was created.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Loginwas created.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Credentials was created.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Loginwas created.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Credentials was created.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Loginwas created.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Credentials was destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Credentials was created.
          
          11:39:43,446 INFO  [STDOUT] @PostConstruct
          
          11:39:43,446 INFO  [STDOUT] Loginwas created.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Loginwas destroyed.
          
          11:39:43,446 INFO  [STDOUT] @PreDestroy
          
          11:39:43,446 INFO  [STDOUT] Credentials was destroyed.




          So you see, CDI or whatever creates and destroyes the needed instances simultanously.


          I am also facing this problem in http://seamframework.org/Community/PostConstructAndPreDestroyOfBean



          So what should I do to get CDI to work?

          • 2. Re: Beans @Inject returns null
            Nicklas Karlsson Master

            Sure you have the cdi and not the jsf version of the annotations?

            • 3. Re: Beans @Inject returns null
              nimo stephan Master

              Oh, you are right.


              I had this:



              Version 1:




              import javax.faces.bean.SessionScoped;
              import javax.faces.context.FacesContext;
              import javax.inject.Inject;
              import javax.inject.Named;
              
              
              @SessionScoped @Named
              public class MyBean {..}






              instead of this:



              Version 2:


              import javax.enterprise.context.SessionScoped;
              import javax.faces.context.FacesContext;
              import javax.inject.Inject;
              import javax.inject.Named;
              
              
              @SessionScoped @Named
              public class MyBean {..}




              Version 2 works.


              But I get no error on deployement with Version 1!


              So it was not easy to find the reason.






              Would be nice, if CDI would warn or returns an deployement-error,


              if it detects a scope of a jsf-version instead of a scope of the cdi-version.
              I should be not allowed to use the scope of a jsf-version in a CDI-Environment - so this should return a deployement error!




              • 4. Re: Beans @Inject returns null
                Nicklas Karlsson Master

                The dual scopes of CDI and JSF are an annoyance. A warning would be handy.