10 Replies Latest reply on Aug 13, 2013 6:03 AM by ili

    Overridden @PostConstruct called twice

    kwutzke

      Hello,

       

      We're having problems with duplicate @PostConstruct calls on a base class hierarchy.

       

      Here's the base class first:

       

      {code}public abstract class AbstractManager<T> implements Serializable

      {

          private List<T> entities;

       

          @PostConstruct // when annotated with @PostConstruct this method is called even if overridden in sub class

          public void init()

          {

              System.out.println( AbstractManager.class.getSimpleName() + " @PostConstruct on " + this.getClass().getSimpleName() + "!" );

          }

         

          protected abstract List<T> getDbEntities();

         

          public List<T> getEntities()

          {

              if ( this.entities == null )

              {

                  this.entities = this.getDbEntities();

              }

             

              return this.entities;

          }

         

          public void setEntities( List<T> entities )

          {

              this.entities = entities;

          }

         

          public void clearEntities()

          {

              this.entities = null;

          }

      }{code}

      Here's the concrete sub class (notice how init() is overridden to call super.init()):

       

      {code}@Named

      @ViewScoped

      public class PseudoEntityManager extends AbstractManager<PseudoEntity>

      {

          private static final long serialVersionUID = 1L;

         

          @Override

          @PostConstruct

          public void init()

          {

              super.init();

          }

       

          ...

      }{code}

      When some (unshown) page is rendered, the pseudoEntityManager bean is instantiated, however @PostConstruct is called twice. This is the output:

       

      {code}    INFO: AbstractManager @PostConstruct on PseudoEntityManager!

          INFO: AbstractManager @PostConstruct on PseudoEntityManager!

          INFO: New list of pseudo DB entities!{code}

      When commenting the overrding init() method in the **concrete sub class** so that there's only one @PostConstruct method from the super class, the following output is generated:

       

      {code}    INFO: AbstractManager @PostConstruct on PseudoEntityManager!

          INFO: New list of pseudo DB entities!{code}

       

      Q:

       

      What's the correct behavior according to CDI specification now? (references anybody?)

       

      Notes:

       

      I also found this mailing list conversation while researching:

       

      http://list-archives.org/2012/10/11/cdi-dev-lists-jboss-org/postconstruct-on-inherited-class/f/4426568582

       

      In the conversation, some gurus tend to say "only the @PostConstruct method on the sub class should be called". If you read closely, there's a link to a Weld bug that's said to be fixed since Weld 1.1.5:

       

      https://issues.jboss.org/browse/WELD-1225

       

      Has this **really** been fixed? According to the output I get, it's not.

       

      Environment: Weld 1.1.8 along with Seam 3 to get the CDI @ViewScoped working correctly (on GlassFish 3.1.2).

       

      Thanks

      Karsten