title: JSF injection with managed property, good pattern?
body: <p>I'm quite new to JSF and not really "used" to the different thinking so I'm struggling on what (I assume) is basic.</p>
  
  <p>Lets say I have a class User, which is a session bean. </p>
  
  <p>Lets say I have a controller of 10000 objects, say Factory, that needs to be able to set some of them as "locked", in our case it means that the "locked" field does not become null anymore but reference a "LockedItem" object.</p>
  
  <p>This is where I can't get things working : LockedItem, when you instanciate it, is supposed to reference the user currently logged in. How am I supposed to do that ?</p>
  
  <p>I tried injection with @managedproperty, but it is null in LockedItem.constructor (which is normal I assume) then I tried in a @PostConstruct method, but that method is never called (why ? Even if I make it a managedbean... are the postconstruct methods only called when the object is created by the ".xhtml" ?)
  Or should I use a "java se" trick, like making the User static ?</p>
  
  <hr>
  
  <p>Code to clarify why is a @PostConstruct not called (the one of "Seat") :</p>
  
  <p><strong>.xhtml</strong></p>
  
  <pre><code>&lt;h:outputLabel id="user" value="Hello #{user.name}" /&gt;
  &lt;h:outputLabel id="car" value="you have #{car.brand}" /&gt;
  </code></pre>
  
  <p><strong>User</strong></p>
  
  <pre><code>package test;
  
  import java.io.Serializable;
  
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  
  @ManagedBean
  @SessionScoped
  public class User implements Serializable {
      private String name ;
  
      public User()
      {
          name = "toto"; 
          System.out.println("User constructor");
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
  }
  </code></pre>
  
  <p><strong>Car</strong></p>
  
  <pre><code>package test;
  
  import java.io.Serializable;
  
  import javax.faces.bean.ManagedBean;
  
  @ManagedBean
  public class Car implements Serializable {
      private String brand ;
      private Seat seat ;
  
      public Car()
      {
          brand = "audi" ;
          seat = new Seat();
          System.out.println("Car constructor") ;
      }
  
      public String getBrand() {
          return brand;
      }
  
      public void setBrand(String brand) {
          this.brand = brand;
      }
  
  
  }
  </code></pre>
  
  <p><strong>Seat</strong></p>
  
  <pre><code>package test;
  
  import java.io.Serializable;
  
  import javax.annotation.PostConstruct;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.ManagedProperty;
  
  @ManagedBean
  public class Seat implements Serializable {
      private int nb ;
      private String userName ;
  
      @ManagedProperty("#{user}")
      private User user ;
  
      public Seat()
      {
          nb = 4 ;
          userName="na";
          System.out.println("! Seat constructor ") ;
      }
  
      @PostConstruct
      public void init()
      {
          System.out.println("!! Seat postconstruct : "+user.getName());
      }
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      public int getNb() {
          return nb;
      }
  
      public void setNb(int nb) {
          this.nb = nb;
      }
  }
  </code></pre>
  
  <p>Thanks !</p>