7 Replies Latest reply on Jul 24, 2008 11:01 PM by mikool

    How to use a seam component in an other one?

    mikool

      Hi guys!


      I know that this a newbie question but I just can't make it work after reading and trying all day.
      I want to use a simple component in an other one.
      First I have this class:

      @Stateless
      @Name("ProcessManager")
      @Startup
      @Scope(ScopeType.APPLICATION)
      public class ProcessManager implements ProcessManagerInterface{
           
                public void dosth(){
                     System.out.println("=====Hello World=====");
                      }
      }


      Then I have this interface:
      public interface ProcessManagerInterface {  
          
           public void dosth();     
           
      }

      Finally I have this test class:
      @Name("testing")
      public class Test {
           
           @In(required=false)
           ProcessManagerInterface processManager;
           
           public void dosth() {
                processManager.dosth();
           }
      
      }

      So, I restarted my server I tried to execute {testing.dosth()} but I always get a null pointer exception.
      Please, tell me how can I use my ProcessManager Component in an other component?


      Thanks in advance.
      Mike

        • 1. Re: How to use a seam component in an other one?
          mail.micke

          Hi


          Seam looks up objects to inject via the name of the property, processManager in you case.


          Since you named it ProcessManager it won't find it.


          To fix that use


          @In("#{ProcessManager}")
          



          You might want to add a


          create=true 
          



          to the injection as well so it is created if doesn't
          already exist.


          Cheers,
          micke

          • 2. Re: How to use a seam component in an other one?
            mikool

            Thanks, it works so far.
            But I cannot add

            create=true

            Can you give me an example how to use
            @In("#{ProcessManager}")

            and add
            create=true

            ?
            Thanks.


            • 3. Re: How to use a seam component in an other one?
              mikool

              I also would like to know if it is possible to solve this problem without using EL in the annotation @In?

              • 4. Re: How to use a seam component in an other one?
                mail.micke

                Seems like you don't have to use EL, looks like just using context variable name should work documentation.


                Example:


                @Name("testing")
                public class Test {
                     
                     @In(value="ProcessManager", required=false)
                     // or I wouldn't use @In(value="#{ProcessManager}", required=false)
                     // or @In(value="ProcessManager", create=true)
                     ProcessManagerInterface processManager;
                     
                     public void dosth() {
                          processManager.dosth();
                     }
                
                }
                



                • 5. Re: How to use a seam component in an other one?
                  mikool

                  If I use

                  @In(value="ProcessManager", required=false)

                  without EL then I get an NullPointerException.

                  • 6. Re: How to use a seam component in an other one?
                    gjeudy

                    It is a mistake to try to give a Seam scope to a stateless session bean. Seam does not manage the lifecycle of the stateless session bean. Moreover I don't think the @Startup annotation works on an EJB for the same reason. Check the seam sourcecode you will see @Startup is only used on POJOs which makes sense.


                    I would try removing @Stateless from your example.


                    @Name("ProcessManager")
                    @Startup
                    @Scope(ScopeType.APPLICATION)
                    public class ProcessManager implements ProcessManagerInterface{
                         
                              public void dosth(){
                                   System.out.println("=====Hello World=====");
                                    }
                    }



                    public interface ProcessManagerInterface {  
                        
                         public void dosth();     
                         
                    }



                    @Name("testing")
                    public class Test {
                         
                         @In(value="ProcessManager")
                         ProcessManagerInterface processManager;
                         
                         public void dosth() {
                              processManager.dosth();
                         }
                    
                    }



                    You could rename your

                    @Name("processManager")

                    and then just:


                    @In
                    processManager


                    should work.


                    If you require a @Stateless EJB then remove @Startup and @Scope and just use:


                    @In(value="ProcessManager", create=true)

                    • 7. Re: How to use a seam component in an other one?
                      mikool

                      Thanks!
                      Now it works very well!
                      But when I don't use

                      @In(create=true)

                      I get a null Pointer Exception.