Tomcat 7,JSF 2.0和@PostConstruct

Osw*_*Osw 3 java jsf tomcat jsf-2

我不知道我做错了什么,请帮忙:

  1. 新鲜的Tomcat 7在/ lib文件夹中没有额外的jar
  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.
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Bean无法访问的方法:

@ManagedBean
@ApplicationScoped
public class AppBean {

  @PostConstruct
  public void test() {
    throw new RuntimeException("test");
  }
}
Run Code Online (Sandbox Code Playgroud)

就这样.有任何想法吗?

Jör*_*ann 5

如果您的应用程序作用域托管bean未在任何页面上使用,则必须使用它进行注释

@ManagedBean(eager=true)
Run Code Online (Sandbox Code Playgroud)

为了在启动时初始化它.