为什么 @PostConstruct 不适用于 Jetty + Apache MyFaces 实现?
Why would @PostConstruct not work with Jetty + Apache MyFaces implementation?
这是我的代码:
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 包中的注释是如何根据我选择的 JSF 实现和我 运行 war 文件中的容器处理/不处理的?
答案是 myfaces 出厂时内置了对 @PostConstruct/@PreDestroy 的支持 Tomcat 并默认启用。
如果您在 运行 jetty maven 插件时查看日志跟踪,您会看到:
Jul 21, 2016 9:49:42 AM org.apache.myfaces.config.annotation.DefaultLifecycleProviderFactory getLifecycleProvider
INFO: Using 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>
这是我的代码:
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 包中的注释是如何根据我选择的 JSF 实现和我 运行 war 文件中的容器处理/不处理的?
答案是 myfaces 出厂时内置了对 @PostConstruct/@PreDestroy 的支持 Tomcat 并默认启用。
如果您在 运行 jetty maven 插件时查看日志跟踪,您会看到:
Jul 21, 2016 9:49:42 AM org.apache.myfaces.config.annotation.DefaultLifecycleProviderFactory getLifecycleProvider INFO: Using 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>