2 Replies Latest reply on Dec 15, 2009 9:09 PM by Mauro Castaldo

    CDI @Dependent scope in Seam

    Mauro Castaldo Newbie

      Does anyone know if it is possible to emulate CDI @Dependent scope in Seam?

      I need to inject a private instances of a Seam component A into other component instances, say C and D.

      I don't want to share the same A instance with component instances C and D.

      For example, if I have:




      public class B {
         @In(create=true) private A a;
         ...
      }
      
      @Name("c")
      @Scope(ScopeType.CONVERSATION)
      public class C extends B {
         ...
      }
      
      @Name("d")
      @Scope(ScopeType.CONVERSATION)
      public class D extends B {
         ...
      }



      I'd like to inject two different instances of A into c and d components.



      I think this is similar to CDI @Dependent pseudo-scope and I find it very useful to be able to inject dependent private helper object instances.


      Is there any way to have the same behaviour in Seam?

        • 1. Re: CDI @Dependent scope in Seam
          German Escobar Newbie

          I think that if you don't define a scope for A, you will be able to achieve that.




          @Name("a")
          public class A {
            ...
          }



          • 2. Re: CDI @Dependent scope in Seam
            Mauro Castaldo Newbie

            Unfortunately if you don't specify any scope you get the default one!


            Seam reference:




            When no scope is explicitly specified, the default depends upon the component type. For stateless session beans, the default is STATELESS. For entity beans and stateful session beans, the default is CONVERSATION. For JavaBeans, the default is EVENT.


            I found a workaround using STATELESS scope and manually creating instance A inside the @Create method of B (I cannot inject a using @In annotation because to keep one instance I need one time injection only during initialization of components c and d):



            public class B {
               ...
            
               @Create
               public void initialize() {
                  this.a = (A) Component.forName("a").newInstance();
                  Component.forName("a").callCreateMethod(this.a);
               }
            
            ...
            }
            



            As you can see, being a an instance of a stateless scoped bean, you have to manually call post-construct / pre-destroy lifecycle methods.

            This don't seems to be a very elegant solution.


            Anyway, thanks for your answer!


            Mauro.