title: Overridden @PostConstruct called twice on CDI view-scoped bean (Weld, Seam 3)
body: <p>We're having problems with duplicate <code>@PostConstruct</code> calls on a base class hierarchy.</p>
  
  <p>Here's the base class first:</p>
  
  <pre><code>public abstract class AbstractManager&lt;T&gt; implements Serializable
  {
      private List&lt;T&gt; 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&lt;T&gt; getDbEntities();
  
      public List&lt;T&gt; getEntities()
      {
          if ( this.entities == null )
          {
              this.entities = this.getDbEntities();
          }
  
          return this.entities;
      }
  
      public void setEntities( List&lt;T&gt; entities )
      {
          this.entities = entities;
      }
  
      public void clearEntities()
      {
          this.entities = null;
      }
  }
  </code></pre>
  
  <p>Here's the concrete sub class (notice how init() is overridden to call super.init()):</p>
  
  <pre><code>@Named
  @ViewScoped
  public class PseudoEntityManager extends AbstractManager&lt;PseudoEntity&gt;
  {
      private static final long serialVersionUID = 1L;
  
      @Override
      @PostConstruct
      public void init()
      {
          super.init();
      }
  
      ...
  }
  </code></pre>
  
  <p>When some (unshown) page is rendered, the <code>pseudoEntityManager</code> bean is instantiated, however <code>@PostConstruct</code> is called twice. This is the output:</p>
  
  <pre><code>INFO: AbstractManager @PostConstruct on PseudoEntityManager!
  INFO: AbstractManager @PostConstruct on PseudoEntityManager!
  INFO: New list of pseudo DB entities!
  </code></pre>
  
  <p>When commenting the overriding <code>init()</code> method in the <strong>concrete sub class</strong> so that there's only one <code>@PostConstruct</code> method from the super class, the following output is generated:</p>
  
  <pre><code>INFO: AbstractManager @PostConstruct on PseudoEntityManager!
  INFO: New list of pseudo DB entities!
  </code></pre>
  
  <p><strong>Q</strong>:</p>
  
  <p>What's the correct behavior according to CDI specification now? (references anybody?)</p>
  
  <h2>Notes:</h2>
  
  <p>I also found this mailing list conversation while researching:</p>
  
  <p><a href="http://list-archives.org/2012/10/11/cdi-dev-lists-jboss-org/postconstruct-on-inherited-class/f/4426568582" rel="nofollow">http://list-archives.org/2012/10/11/cdi-dev-lists-jboss-org/postconstruct-on-inherited-class/f/4426568582</a></p>
  
  <p>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 resolved since Weld 1.1.5:</p>
  
  <p><a href="https://issues.jboss.org/browse/WELD-1225" rel="nofollow">https://issues.jboss.org/browse/WELD-1225</a></p>
  
  <p>Has this <strong>really</strong> been fixed? According to the output I get, it's not.</p>
  
  <p>Environment: Weld 1.1.8 along with Seam 3 to get the CDI @ViewScoped working correctly (on GlassFish 3.1.2).</p>