4 Replies Latest reply on Nov 10, 2009 10:00 PM by Tony Brouwer

    PostConstruct and Stateless scoped components

    Tony Brouwer Newbie
      Stateless Seam components do not invoke their postconstruct methods (as advertised). Same thing for @Create. Is this a bug in Seam?

      To illustrate:

      @Name("myStatelessComponent")
      @Scope(ScopeType.STATELESS)
      public class MyStatelessComponent {

          private String myState = "uninited";
         
          @PostConstruct
          public void init() {
              myState = "inited";
          }
         
          public String whatsMyState() {
              return myState;
          }
      }

      Running a little integration test shows that myState equals "uninited":

      `    @Test
          public void doesMyStatelessComponentWork() throws Exception {
              // setupClass();
              new FacesRequest() {
                  @Override
                  protected void invokeApplication() {
                      MyStatelessComponent msc =
                          (MyStatelessComponent) Component.getInstance(
                                  "myStatelessComponent");
                      assert (msc.whatsMyState().equals("inited")) : "Expected inited, found " + msc.whatsMyState();
                  }

              }.run();
          }


      `

        • 1. Re: PostConstruct and Stateless scoped components
          Shervin Asgari Master

          In java ee 5 you need to implement an interface for the stateless ejb.




          public interface MyStatelessComponentManager {
          
             public void init();
          
             public String whatsMyState();
          }
          
          //Try this
          public class MyStatelessComponent implements MyStatelessComponentManager {}



          @PostConstruct should work just fine.

          • 2. Re: PostConstruct and Stateless scoped components
            Shervin Asgari Master

            Forgot to say that you need to do this after you implement interface.




            MyStatelessComponentManager msc =
                                (MyStatelessComponentManager) Component.getInstance(
                                        "myStatelessComponent");



            It will give you an implementation of your MyStatelessComponentManager which is (MyStatelessComponent)

            • 3. Re: PostConstruct and Stateless scoped components
              Tony Brouwer Newbie

              My example is not a stateless session bean. It is a stateless context seam component. According to my Seam in Action book, Stateless context is a nonstoring context. Forces a component to be instantiated each time the component name is resolved. Equivalent to Spring's prototype scope.


              Another example of a stateless context component is org.jboss.seam.persistence.HibernatePersistenceProvider.class, which, being a stateless component, has the same problem: it does not invoke the @PostConstruct method on construction.

              • 4. Re: PostConstruct and Stateless scoped components
                Tony Brouwer Newbie

                Found the mistake.


                I used this PostConstruct annotation:


                import org.jboss.seam.annotations.intercept.PostConstruct;
                



                Seam Component class is looking for:


                import javax.annotation.PostConstruct;
                



                As a result the Component class was not picking up my postconstruct method.


                I based my stateless component on the Seam PersistenceProvider class (class org.jboss.seam.persistence.PersistenceProvider), which also uses the first PostConstruct annotation for it's init method.


                PersistenceProvider therefore has the same error. PersistenceProvider.init() is therefore also not invoked postconstruct. It must be modified to reference javax.annotation.PostConstruct instead.


                This is a bug in the current Seam library: 2.2.0.GA.