Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The point of <code>@PostConstruct</code> is to provide a hook to execute some code <em>after</em> all managed properties (as in <code>&lt;managed-property&gt;</code> or <code>@ManagedProperty</code>) are been set and all dependency injections (e.g. <code>@EJB</code>, <code>@Resource</code>, <code>@Inject</code> and so on) have taken place.</p> <p>If you don't have any of them, just use the bean's constructor.</p> <pre><code>public class Bean { public Bean() { // Just do your job here. Don't do it the hard way. } // ... } </code></pre> <p>Or if you actually want to execute it when a specific property has been set, then do the job in the setter while null-checking the current property value.</p> <pre><code>public class Bean { private SomeObject someManagedProperty; public void setSomeManagedProperty(someManagedProperty) { if (this.someManagedProperty == null &amp;&amp; someManagedProperty != null) { // First-time set, now you can do your job here. } this.someManagedProperty = someManagedProperty; } // ... } </code></pre> <hr> <p><strong>Update</strong> as per the comments:</p> <blockquote> <p><em>I meant to execute the method every time the page is loaded</em></p> </blockquote> <p>The <code>@PostConstruct</code> doesn't do that. However, if the bean is request scoped, then you will see the same effect. You seem to be using a session or application scoped managed bean to manage request scoped data. This is in essence wrong. You should convert it to a request scoped bean. Any <em>real</em> session scoped data can be split into a session scoped bean which you then inject by <code>&lt;managed-property&gt;</code>. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload