9 Replies Latest reply on Aug 17, 2011 4:59 AM by Jason Porter

    Inject Request beans into a servlet fails

    Craig  Newbie

      I have a servlet (File upload) that I need to call a method on a request scoped bean.


      The request bean is injected but when I access the method I get



      org.jboss.weld.exceptions.IllegalStateException: WELD-001304 More than one context active for scope type javax.enterprise.context.SessionScoped

      Anyone any ideas?


      Thanks


      Craig

        • 1. Re: Inject Request beans into a servlet fails
          Jason Porter Master

          Sounds like something is really broken. Would you paste some code please?

          • 2. Re: Inject Request beans into a servlet fails
            Craig  Newbie

            I think this is a result of a producer method...


            I have a @SessionScoped bean that observes a LoginEvent.


            This bean then has a method that produces the User, using @Produces and @LoggedIn.


            This value is injected into a @Stateless bean as @Inject @Logged User.


            If remove the User injection all works fine.


            Any ideas?


            Many thanks


            Craig

            • 3. Re: Inject Request beans into a servlet fails
              Jason Porter Master

              Craig   wrote on Aug 16, 2011 00:05:


              I think this is a result of a producer method...

              I have a @SessionScoped bean that observes a LoginEvent.

              This bean then has a method that produces the User, using @Produces and @LoggedIn.

              This value is injected into a @Stateless bean as @Inject @Logged User.

              If remove the User injection all works fine.

              Any ideas?

              Many thanks

              Craig


              If the producer produces @LoggedIn and you're injecting @Logged that won't work. Try changing the qualifiers on one or the other.

              • 4. Re: Inject Request beans into a servlet fails
                Craig  Newbie

                Sorry that was a typo


                Both produces and inject used @LoggedIn (not the seam version, this is an application annotation)


                Thanks


                Craig

                • 5. Re: Inject Request beans into a servlet fails
                  Jason Porter Master

                  Could you please paste some code for the injection point and the producer?

                  • 6. Re: Inject Request beans into a servlet fails
                    Craig  Newbie

                    Sure, thanks for you help...


                    @SessionScoped
                    public class SessionContext implements Serializable
                    {
                         @Inject
                         private Logger log;

                         private static final long serialVersionUID = 1L;

                         private Account account;
                         private User user;

                         public void userAuthenticated(@Observes @LoggedIn User user)
                         {
                              this.user = null;
                              
                              log.infov("User swapped to : {0}", user.getUsername());
                                   
                              this.user = user;     
                              this.account = user.getAccount();
                         }
                         
                         public void userLoggedOut(@Observes @LoggedOut User user)
                         {
                              this.user = null;
                         }

                         @Produces
                         @LoggedIn
                         public User getUser()
                         {          
                              return user;
                         }

                         @Produces
                         @LoggedIn
                         public Account getAccount()
                         {          
                              return account;
                         }
                    }


                    @WebServlet(urlPatterns="/resource", loadOnStartup=1)
                    public class ResourceServlet extends HttpServlet {

                         private static final long serialVersionUID = 1L;

                         @Inject
                         private ResourceManager resourceManager;

                         ........

                    }



                    @RequestScoped
                    public class ResourceManager implements Serializable {

                         private static final long serialVersionUID = 1L;
                         
                         @Inject
                         @LoggedIn
                         private Account account;

                         ........
                    }



                    Thanks again


                    Craig


                    • 7. Re: Inject Request beans into a servlet fails
                      Jason Porter Master

                      Sorry, going to ask you to repaste and use ` to surround your code :)

                      • 8. Re: Inject Request beans into a servlet fails
                        Craig  Newbie

                        Sorry my bad, should have previewed...




                        @SessionScoped 
                        public class SessionContext implements Serializable { 
                             
                             @Inject private Logger log; 
                             
                             private static final long serialVersionUID = 1L; 
                             private Account account;
                             private User user; 
                             
                             public void userAuthenticated(@Observes @LoggedIn User user) 
                             { 
                                  log.infov("User swapped to : {0}", user.getUsername()); 
                                  this.user = user; 
                                  this.account = user.getAccount(); 
                             } 
                             
                             public void userLoggedOut(@Observes @LoggedOut User user) 
                             { 
                                  this.user = null; 
                                  
                             } 
                             
                             @Produces 
                             @LoggedIn 
                             public User getUser() 
                             { 
                                  return user; 
                             } 
                             
                             @Produces 
                             @LoggedIn 
                             public Account getAccount() 
                             { 
                                  return account; 
                             } 
                        }





                        @WebServlet(urlPatterns="/resource", loadOnStartup=1) 
                        public class ResourceServlet extends HttpServlet 
                        { 
                        
                             private static final long serialVersionUID = 1L; 
                             
                             @Inject 
                             private ResourceManager resourceManager; 
                             
                             ........ 
                        }




                        @RequestScoped 
                        public class ResourceManager implements Serializable 
                        { 
                             private static final long serialVersionUID = 1L; 
                             
                             @Inject 
                             @LoggedIn 
                             private Account account; 
                             
                             ........ 
                             
                        }