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

    Inject Request beans into a servlet fails

    craig

      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
          lightguard

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

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

            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
              lightguard

              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

                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
                  lightguard

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

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

                    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
                      lightguard

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

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

                        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; 
                             
                             ........ 
                             
                        } 




                        • 9. Re: Inject Request beans into a servlet fails
                          lightguard

                          It may be possible you're running into https://issues.jboss.org/browse/WELD-892