Tomcat 7, JSF 2.0 and @PostConstruct

I do not know what I am doing wrong, please help:

  • Fresh Tomcat 7 without additional cans in the / lib folder
  • Simple web application with mojarra 2.0.3 files in WEB-INF / lib (jsf-api.jar, jsf-impl.jar)
  • Works great except @PostConstruct in my beans - they are 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 an unreachable way:

@ManagedBean
@ApplicationScoped
public class AppBean {

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

It's all. Any ideas?

+3
source share
2 answers

If your bean control app is not used on any pages, you should annotate it with

@ManagedBean(eager=true)

to initialize it at startup.

+5
source

@PostConstruct , beans, eager = true init .

0

All Articles