4 Replies Latest reply on Nov 16, 2011 12:25 PM by Drew A

    CDI General Question

    Drew A Newbie

      I am working through the JBOSS A7 Quickstart examples and I have a basic question....  first let me give the code:


      //Login.java



      import java.io.Serializable;
      
      import javax.enterprise.context.SessionScoped;
      import javax.enterprise.inject.Produces;
      import javax.faces.application.FacesMessage;
      import javax.faces.context.FacesContext;
      import javax.inject.Inject;
      import javax.inject.Named;
      
      @SessionScoped
      @Named
      public class Login implements Serializable {
      
         private static final long serialVersionUID = 7965455427888195913L;
      
         @Inject
         private Credentials credentials;
      
         @Inject
         private UserManager userManager;
      
         private User currentUser;
      
         public void login() throws Exception {
            User user = userManager.findUser(credentials.getUsername(), credentials.getPassword());
            if (user != null) {
               this.currentUser = user;
               FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Welcome, " + currentUser.getName()));
            }
         }
      
         public void logout() {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Goodbye, " + currentUser.getName()));
            currentUser = null;
         }
      
         public boolean isLoggedIn() {
            return currentUser != null;
         }
      
         @Produces
         @LoggedIn
         public User getCurrentUser() {
            return currentUser;
         }
      
      }





      Why is @LoggindIn a Qualifier interface?  Why not use a class like Credentials earlier in the code?  What's the point of using @Produces?  Why not just @Inject some class?


      Thanks,


      Drew

        • 1. Re: CDI General Question
          Jason Porter Master

          There may be other Users such as one which has not logged in, this further qualifies which instance you want to inject.


          The @Produces creates the @LoggedIn User so you can @Inject it.

          • 2. Re: CDI General Question
            Drew A Newbie

            Jason,


            Thanks for the response!  I think I get what you are saying, but I still am a little shakey on the concept of injection in general I guess.  So the @Produces creates the @LoggedIn User so you can @Inject it.  I guess now I have a more fundemental question... Why inject?  Isn't injecting simply invoking the getCurrentUser() method on the instantiation of the Login class?  Why can't this be done with some call in a constructor?  Or even using @PostConstruct? Is this all needed because beans can not have parameterized constructors?


            Thanks for you time.


            --Drew

            • 3. Re: CDI General Question
              Drew A Newbie

              Ok, hold on... I think I need to re-phrase.  Fields/Methods with @Inject get invoked on instation of the class.  I guess @Produces is for a user of the class?  Who can use this implemetation of @LoggedIn?  What if another class has a different @Produces @LoggedIn implementation?

              • 4. Re: CDI General Question
                Drew A Newbie

                Jason,


                I realize the previous questions reeked of ignorance.  I am in the process of digging into CDI for the first time.  I am reading the Weld - JSR-299 Reference Implementation now.  I didn't realize there was good literature out there on this topic.... http://docs.jboss.org/weld/reference/latest/en-US/html/index.html.


                I'm sure the guide will clear things up for me.


                Thanks so much for your response.


                --Drew