8 Replies Latest reply on Jun 11, 2008 9:46 PM by samdoyle

    How can I do this with Seam?

    samdoyle

      I want to have only one instance of a Seam component per session let's call it global. This global instance needs to be available to all pages since they may or may not choose to have it injected. If they do inject it, it needs to be the same instance always across all pages. This single global instance would also need to the one obtained when using Seam.Component.getInstance(global).


      Thanks

        • 1. Re: How can I do this with Seam?
          monkeyden

          I'm confused...is it global (application) or session scoped?  Also not sure what you mean by across all pages


          Based on my understanding of the question, the answer is yes.  You would create a seam component as such:


          @Name("global")
          @Scope(ScoreType.SESSION) //or ScoreType.APPLICATION
          @Startup //creates one when session/application context is created
          public class GlobalThingy{
          ...
          }

          • 2. Re: How can I do this with Seam?
            monkeyden

            Doh!


            ScoreType = ScopeType

            • 3. Re: How can I do this with Seam?
              samdoyle

              Hello and thanks no I meant session scoped. And I did try to apply the @Startup to the component as you mentioned but then I try to inject it I get the @In attribute requires non-null value: exception


              So as you mentioned this is what I have:



              @Stateful
              @Startup
              @Scope( ScopeType.SESSION )
              @Name( "global" )




              Then in another class I have for example using the name Global



              @In( required = true )
              Global global;




              I had thought the @Startup would provide what I needed but it doesn't appear to do so.

              • 4. Re: How can I do this with Seam?
                monkeyden

                I would guess that @Startup would imply @AutoCreate but, based on what I'm hearing from you, I guess not.  Try adding @AutoCreate at the class level.


                @Name("global")
                @AutoCreate
                @Scope(ScoreType.SESSION) //or ScoreType.APPLICATION
                @Startup //creates one when session/application context is created
                public class GlobalThingy{
                ...
                }

                • 5. Re: How can I do this with Seam?
                  samdoyle

                  Ok there is something really strange happening here or there is something about Seam I'm missing.


                  I have annotated the setter method with @In


                  Even with the injected value being required I will still hit a case where a null value is passed in. The first injection is fine but then the second time it is null. So as an example using Global:


                  @In
                  public void setGlobal( Global global)
                  {
                        global.setListener( this );
                  }

                  • 6. Re: How can I do this with Seam?
                    samdoyle

                    Alright so I was able to accomplish what I was trying to do thanks for your help. However, I must explicitly check for a null value passed into the @In setter even though it is required. Is this a bug?

                    • 7. Re: How can I do this with Seam?
                      monkeyden

                      Post the code for both components

                      • 8. Re: How can I do this with Seam?
                        samdoyle

                        Thanks, unfortunately I can't post the entire code I would have to refactor all the names :). However, I guess the basic question is if you have a setter method for an injected value annotated with @In shouldn't you always be guaranteed it isn't null and if it is you should get an exception? In my case there is a null value passedin.