3 Replies Latest reply on Jun 21, 2011 4:46 AM by Jason Porter

    PoJo postInitialization

    Joe Schiavone Newbie

      Can anyone provide me a better understanding of how postinitialization is supposed to work in Seam 3??   I have tried a variety of solutions that I found in the forums and websites, none of which work exactly like the seam 2 postinitialization...  Specifically I would like to do postInitialization of a managedbean, altering whatever is set up in its default constuctor.  


      Does not work:::




      @Named
      @ApplicationScoped
      
      public class start {
      @Inject PoJo poJo;
      public void onStartup( @Observes @Started WebApplication webApplication ) 
      pojo = new PoJo("Alter","default",String","fields");
      }





      When I inject the PoJo class into any subsequent class the fields are not set as they should have been with the onStartup method.




        • 1. Re: PoJo postInitialization
          Sebastian Sachtleben Novice

          If you want to inject your pojo class you will need a producer like this:


          @ApplicationScoped
          public class PojoProducer {
          
            private Pojo pojo;
          
            @Produces
            public Pojo getPojo() {
              return pojo;
            }
          
            public void onStartup(@Observes @Started WebApplication webApplication) {
              pojo = new Pojo(...);
            }
          
          }



          Btw you dont need @Named on this class because you can directly use

          #{pojo}

          in facelets.

          • 2. Re: PoJo postInitialization
            Sebastian Sachtleben Novice

            Ah sorry false information. You can Inject without @Named, because it is a managed bean anyways if you dont at @Veto to the class. But if you want to use it in facelets you will need to add @Named to the @Produces method.


            Thats it :)

            • 3. Re: PoJo postInitialization
              Jason Porter Master

              You could do a Producer like Sebastian is saying, but I think what you're looking for is something like this (if you're looking for the post construct per bean):




              @Named
              @ApplicationScoped
              public class MyClass {
                  @Inject
                  public void myMethodName(any injections wanted as params) {
                      ...
                  }
              }



              Basically any method annotated with @Inject (probably should be void, not 100% if that's required though) will be use for post construct. Best practice should not to use constructors for setup as they'll be called twice due to subclassing.