Tomcat 7, JSF 2.0 and @PostConstruct

3

我不知道我哪里做错了,请帮忙:

  1. 在/lib文件夹中没有额外的jar包的新鲜Tomcat 7
  2. 在WEB-INF/lib中使用mojarra 2.0.3库的简单Web应用程序(jsf-api.jar,jsf-impl.jar)
  3. 除了我的bean中的@PostConstruct不被调用之外,一切正常

日志:

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:

@ManagedBean
@ApplicationScoped
public class AppBean {

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

这就是全部。有什么想法吗?

整个堆栈跟踪会有所帮助。通过一些快速的搜索,我怀疑那不是问题所在。 - Brian Roach
@Brian Roach,实际上整个堆栈是在我提到的那行代码之前的“Mar 12, 2011 11:19:54 PM com.sun.faces.config.ConfigureListener contextInitialized INFO:为上下文'/test_web_app'初始化Mojarra 2.0.3(FCS b03)”。 - andbi
2个回答

5

如果您的应用程序作用域管理的bean没有在任何页面使用,您需要使用注释对其进行标注:

@ManagedBean(eager=true)

为了使其在启动时初始化,需要进行以下操作。

0
如果bean已加载,@PostConstruct应该可以工作。您可以在直接调用时或使用eager=true进行加载,或在构造函数中进行初始化。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接