Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat 7, JSF 2.0 and @PostConstruct

I have no idea what I'm doing wrong, please help:

  1. Fresh Tomcat 7 with no extra jars in /lib folder
  2. Simple web-app with mojarra 2.0.3 libs in WEB-INF/lib (jsf-api.jar, jsf-impl.jar)
  3. Works fine except @PostConstruct in my beans - they're not called at all

Logs:

Mar 12, 2011 11:19:54 PM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive test_web_app.war
Mar 12, 2011 11:19:54 PM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.0.3 (FCS b03) for context '/test_web_app'
Mar 12, 2011 11:19:54 PM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans methods marked with these annotations will have said annotations processed.

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">

  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>

</web-app>

faces-config.xml

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
              version="2.0">
</faces-config>

Bean with unreachable method:

@ManagedBean
@ApplicationScoped
public class AppBean {

  @PostConstruct
  public void test() {
    throw new RuntimeException("test");
  }
}

That's all. Any ideas?

like image 451
andbi Avatar asked Dec 07 '25 04:12

andbi


1 Answers

If your application scoped managed bean is not used on any pages, you have to annotate it with

@ManagedBean(eager=true)

in order to get it initialized on startup.

like image 51
Jörn Horstmann Avatar answered Dec 08 '25 17:12

Jörn Horstmann