提问者:小点点

为什么@PostConstruct不适用于Jetty Apache MyFaces实现?


这是我的代码:

pom. xml

<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>javax.faces-api</artifactId>
    <version>2.2</version>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.2.13</version>
</dependency>
<build>
    <finalName>darbe</finalName>
    <plugins>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>9.2.1.v20140609</version>
            <configuration>
                <scanIntervalSeconds>2</scanIntervalSeconds>
                <webApp>
                    <contextPath>/</contextPath>
                </webApp>
            </configuration>
        </plugin>
    </plugins>
</build>

web. xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>FacesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>FacesServlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>foo.xhtml</welcome-file>
    </welcome-file-list>

</web-app>

foo. xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
    <title>Foo</title>
</h:head>
<h:body>
    <h:outputText value="#{foo.bar}"/>
</h:body>
</html>

最后Foo.java

package biz.tugay;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class Foo {

    private String bar;

    @PostConstruct
    public void init() {
        bar = "Hello World";
    }

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }
}

所以在这里我使用的是Oracle的JSF实现。当我构建这个项目并将其部署到Tomcat或使用Jetty插件(mvn jetty: start)时,我会在浏览器上看到文本Hello World。

但是,当我更改依赖项时,如下所示:

<dependency>
    <groupId>org.apache.myfaces.core</groupId>
    <artifactId>myfaces-api</artifactId>
    <version>2.2.10</version>
</dependency>
<dependency>
    <groupId>org.apache.myfaces.core</groupId>
    <artifactId>myfaces-impl</artifactId>
    <version>2.2.10</version>
</dependency>

只有当我使用maven构建war文件并将其部署到Tomcat时,我才会看到Hello World。当我使用mvn jetty: start运行应用程序时,@PostConstruct将永远不会被调用,因此foo.bar将为空。

这是怎么回事?如何根据我选择的JSF实现和我运行war文件的容器来处理/不处理javax.注解包中找到的注解?


共1个答案

匿名用户

答案是我的面孔开箱即用,内置了对Tomcat的@PostConstruct/@PreDestroy的支持,并默认启用。

如果您在使用jetty maven插件运行时查看日志跟踪,您会看到:

2016年7月21日上午9:49:42 org.apache.myfaces.config.annotation.DefaultLifeycleProviderFactory getLifeycleProvider信息:使用LifeycleProvider org.apache.myfaces.config.annotation.Tomcat7AnnotationLifeycleProvider

所以我的脸发现Tomcat LifeCycleProvider烘焙到它的impljar中,并默认使用它。

解决方案是显式地告诉我的面孔使用不同的LifeCycleProvider。我使用了org. apache.myfaces.config.annotation.NoInjsionAnnotationLifeCycleProvider(在impljar中),它看起来很有效,但可能值得在我的面孔列表中询问更多信息。要设置LifeCycleProvider,请将其添加到您的web.xml中:

          <context-param>
            <param-name>
              org.apache.myfaces.config.annotation.LifecycleProvider
            </param-name>
            <param-value>
              org.apache.myfaces.config.annotation.NoInjectionAnnotationLifecycleProvider
            </param-value>
          </context-param>