Monday, 14 January 2013

Jboss 7, not triggering @PostConstruct

Whilst porting a JSF 2 project over to Jboss 7 I came up against a problem of the @PostConstruct not getting triggered within a @ManagedBean. 

So have:


@ManagedBean
@ViewScoped
public class MyController {

  public String inp;

  @PostConstruct
  public void init() {
    // do something
  }

  public void setInp( String inp ) {
    this.inp = inp;
  }

  public String getInp() {
    return inp;
  }
}


Solution - Add the following to the web.xml:


<context-param>
 <param-name> 
   com.sun.faces.injectionProvider</param-name>
 <param-value>
   com.sun.faces.vendor.WebContainerInjectionProvider    
 </param-value>
</context-param>

The Jboss provider also works:
org.jboss.web.jsf.integration.injection.JBossInjectionProvider



See here for a detailed discussion.