【问题标题】:Why would @PostConstruct not work with Jetty + Apache MyFaces implementation?为什么@PostConstruct 不适用于 Jetty + Apache MyFaces 实现?
【发布时间】:2016-11-24 02:57:22
【问题描述】:

这是我的代码:

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 将为空。

这里发生了什么? javax.annotation 包中的 Annotation 如何根据我选择的 JSF 实现和运行 war 文件的容器处理/未处理?

【问题讨论】:

    标签: maven jsf tomcat jetty


    【解决方案1】:

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

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

    2016 年 7 月 21 日上午 9:49:42 org.apache.myfaces.config.annotation.DefaultLifecycleProviderFactory getLifecycleProvider 信息:使用 LifecycleProvider org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider

    所以 myfaces 发现 Tomcat LifeCycleProvider 被烘焙到它的 impl jar 中,并默认使用它。

    解决方案是明确告诉 myfaces 使用不同的 LifeCycleProvider。我使用了 org.apache.myfaces.config.annotation.NoInjectionAnnotationLifecycleProvider (在 impl jar 中),它似乎可以工作,但可能值得在 myfaces 列表中询问更多信息。要设置 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 2011-03-25
      相关资源
      最近更新 更多